Basic Foundation Of JavaScript Part 2:(Primitive Data Types)
Welcome back to the continuation of the Basic JavaScript Series (Part-2). In this section, i’m going to be talking about Primitive Data Types.
Primitive Data Type is an important aspect of programming, its one of the building fundamentals on how we write our code in JavaScript. often time its over looked, but its important to understand what they really do.
Every Programming Language could be thought of as consisting a set of primitives, a means of combination of putting things together to create an expression to do something for us. Its important to note, that not everything is an Object. Primitive data types is not an object , has no methods and are immutable.
In JavaScript there are six Primitive Data Types
- String
- Number
- Boolean
- Null
- Undefined
- Symbols
Strings
Strings are sequence of character, identified by either single or double quotes. it could be numbers or characters. just like in English or any other subjects we write, things so we can read and understand what we have written. Thats how strings work: A collection of characters written to interpret or communicate something to us in our program. For example, if you build an app, you definitely want people to know what your app does. How will you be able to express that? It is represented using “string”.
Here is a simple illustration.
Numbers
In JavaScript there are is no difference between floating point numbers, decimal and integers. In JavaScript all are represented as Numbers. Number in JavaScript represents integers, float, hexadecimal, octal or exponential value.Unlike other programming language where, there is a difference between these numbers. There are many operations for numbers, e.g. multiplication *
, division /
, addition +
, subtraction -
, etc and to mention but a few.
Boolean
Boolean is only represented with two values true or false. its basically used to control program flow or compare using conditional statements like if..else, switch, while, do..while.
Null
Null basically means absence of a value. For example: There might be a time where you can assign null to a variable to show that, the variable does not have a value, but later on in your code can be assigned a value.
In the above example, null is assigned to a variable assign. However this means a variable was declared but a value has not yet be declared. When this is logged into the console it results in absence of value. Null is also of an object type.
Here is an illustration.
A Null value always results to false in conditional expression. It might not be necessary to make use of comparison operators e.g === or !== to confirm if it’s a null value.
Undefined
Null and undefined are often been used interchangeably, but it’s important to note that the both are different. Remember null means absence of value. However undefined is when a variable or an object has an undefined value, when no value has been assigned before using it. Undefined simply means lack of value or unknown value.
However, the above example illustrates that the variable assign has not been given a value yet. Which indicates that ‘assign’ lacks a value, which makes it undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns Undefined if a value was not Returned.
The above example illustrates the function num, num does not return any result, even though when reassigned a new variable sum. However, the result will be undefined. Why? Because sum has not been called.
It’s important that when declaring your variable , you assign it a value. Given that if you don’t it will return an error.
Lastly Symbols- Symbol is a primitive data type, it was introduced in ECMAScript 2015. It’s a very unique data type. The symbol value is used as an identifier for object properties; however this is it primary purpose.
That’s a wrap for now!😊
The next series I will be talking about Operators.
Stay tuned!😀