Data Types in JavaScript

As we have seen in previous chapter, a variable in JavaScript can contain any data type. at one moment, variable can contain a string and at second-moment same variable can contain an integer.
But that doesn't mean JavaScript doesn't have any data types. There are 7 data types in JavaScript. We can divide it into two categories.

  • Primitives
  • Object

Primitive Datatype

A primitive is a data that is not an object and has no methods. In Javascript, there are 6 primitive data types.

boolean ( Logical type )

The boolean type has only two values: true and false.
Noramally true means 'yes' and false means 'no'.
Most of the times, boolean used for logical operation and in if..else...


let isMore = 100 > 99;
console.log(isMore);
//output: true
if(isMore){
    console.log("100 is more than 99");  
} else {
    console.log("99 is more than 100");
}
//output: "100 is more than 99"
                    

Right now, don't worry about the if...else part.

number type

In JavaScript, Number is a numeric data type in the double-precision 64-bit floating point format (IEEE 754). There are 5 types of number.

  • Integer
  • Floating
  • Infinity
  • -Infinity
  • NaN (Not a Number)

There are many operations for numbers, e.g. multiplication *, division /, addition +, subtraction - and so on.
Here are a few operations related to numbers.


console.log( 1 / 0 );
//output: Infinity

console.log( -1 / 0 );
//output: -Infinity

//Any mathematical operation (Apart from +) on non-numeric enitity will result in NaN
console.log("This is easy" / 2);
//output: NaN

console.log( "this is it" / 0 );
//output: NaN
                    

Any further operation on NaN gives NaN. NaN is mostly used for comparing the result of the any mathematical operation to check for the error in computation.

string type

String in JavaScript is quoted.
There are three kinds of quotes in JavaScript.

  • Double quotes "Hello"
  • Single quotes 'Hello'
  • Backticks `Hello`

Backticks were introduced EcmaScript 6 for string interpolation. Previously string interpolation can only be done by + operator. Please have a look at below operation for more info.


let string1 = "Hello";
let string2 = "World";

//String interpolation
console.log(string1 + string2);
//output: "HelloWorld"

let string3 = `World`;

//String interpolation
console.log(`Hello ${string3}`);
//output: "Hello World"
//Note: it is not needed that string3 must be declared using backtics. you can use backticks for 
// interpolation with the string declared using single quotes or double quotes.

console.log(`${string1} ${string2}`);
//output: Hello World

//You also can embed expression in ${}
console.log(` 1 + 2 is ${1+2}`);
//output: 1 + 2 is 3

console.log(`Hello ${'World' + ' World2'}`);
//output: Hello World World2
                    

The expression in ${...} is evaluated. We can put expression like 1+2 or something more complex.
There is no difference between a single quoted string and double-quoted string.

null type

null is the special keyword denoting nothing value. null is the special kind of value of its own. It doesn't have any of the above value. Since JavaScript is case-sensitive language, null is not same as Null or NULL.
null is intentionally given value indicating that the variable holds nothing.


let message = null;
                    

undefined type

undefined is value of its own just like null.
Any variable that is declared but value is not assigned ( is not defined ) has undefined vlaue.


let message;
console.log(message);
//output: undefined

let message1 = undefined;
console.log(message1);
//output: undefined
                    

It is possible to assign undefined value to any variable.

symbol type

symbol is the data type which primarily used to make object properties that are anonymous. symbol is used when you want to make the property of the object private. You will know better when you have learned about object in detail.


// To create symbol, use Symbol()
let privateKey = Symbol();

let obj = {};
obj[privateKey] = "property won't appear";
obj['key'] = "property will appear";

console.log(obj);
//output: {key: "property will appear", Symbol(): "property won't appear"}
                    

object

object is something that referes to real world thing. In JavaScript, object contains key and value pair. In object, key means property. For every key, there is a value assigned. That value can be any data ( string, number or boolean ) or it can be function.
There are many ways to create object, but right now, for starters, We will use only one.


let emptyObj = {}; // An empty Object
//output: {}

emptyObj["str"] = "value"; // Add key value pair

console.log(emptyObj);
//output: {str:"value"}

//You can directly add key-value pair
let anotherObj = {
    "key":"value"
};

console.log(anotherObj);
//output: {key:"value"}
                    

This is the basic part of object. You will be learning about it in coming sections.

Conclusion

In this section, you learned about 6 primitive data types and object.
In next section, you will learn about typeof operator in JavaScript.