Data Types in Javascript

In any programming language, there are variables and variables store values. These values can be of different types.

In Javascript there are 8 different types of data types. Data types define the type of data that can be stored in the variable and used in the program. These 8 different data types are classified into two types primitive and non-primitive data types.

Primitive data types: These are basic data types that represent a single value. The following are the primitive data types in JavaScript:

·         Number

·         String

·         BigInt

·         Boolean

·         Undefined

·         Null

·         Symbol (added in ECMAScript 6) 

Non-primitive data types: These are complex data types that represent a collection of values. The following is the non-primitive data type in JavaScript:

·         Object: including arrays, functions, and more.

 The main difference between primitive and non-primitive data types is that primitive data types are immutable (i.e., their values cannot be changed), while non-primitive data types are mutable (i.e., their values can be changed). For example, when you assign a new value to a primitive data type, a new memory location is created to store the new value, while when you change a property of an object, the same memory location is updated with the new value.

 

The following are different data types in Javascript:

1. Number

A Number data type implies both positive or negative numbers as well as floating point numbers. It is also used to represent numbers in exponential form like 2.3e-7 (equivalent to 2.3x10-7).


Some special numerical values like Infinity, -Infinity, NaN are also included in the Number data type.

We get Infinity value which is a number greater than zero when we divide any number with zero.

alert(12/0); // infinity

alert(-12/0); // infinity

alert(12/-0); // infinity

NaN ( Not a Number) is a special value that is obtained due to a undefined mathematical operation.

alert (“NaN”/6) //NaN


2. String

String data type represents textual data or string of characters that are enclosed within single or double quotes. For example “Apple”, ‘Orange’ etc. Single or double quotes are not different and indicate same thing.


3. Boolean

Boolean data type can only contain two values i.e. either True or False.

True means ‘Yes’ and False means ‘No’.

You can also get Boolean values as a result of some comparison.


4. Null

Null data type in javascript literally represents “nothing”. However, if  you use typeof operator for null, it returns an object type. This is considered a bug in javascipt.


5. Undefined

The Undefined data type is also stand alone data type like Null and it actually means “value is not assigned”. When you just declare a variable but do not assign any value to it, then it has undefined value.


6. BigInt

The Number data type in Javascript cannot properly store integer values greater than (253 + 1) and less than -(253 - 1). If we try to store value greater than these values then a error gets displayed. To accommodate integers of arbitrary length, we use BigInt type.

Syntax

BigInt ( number)

OR

By appending ‘n’ at the end of the number.

Const biggInt = 023049823904120980948302984320948n;


7. Symbol

The Symbol data type which was newly introduced in ES6 is used to create a unique identifier key that cannot be changed.

We use Symbol() to create a new primitive symbol data type.


Two Symbols with same description are Unique

Two symbols with same description are not same even they look same.

For example:

let x1 = Symbol(“hi”);

let x2 = Symbol (“hi”);

console.log(x1 === x2); //false

Add Symbol as an Object Key

You can also add Symbol as an Object Key as shown below

You can add symbols as a key in an object using square brackets []. For example,

 

let rollno = Symbol("rollno");

let person = {

    name: "Nikhil",

    // adding symbol as a key

    [rollno]: 444 // not "rollno": 444

};

console.log(person); // {name: "Nikhil", Symbol(rollno): 123}

 The best thing about Symbol is that they are unique and immutable. You can use the same unique Symbol key in another code without duplication issues.

8. Object

The Object data type is a non-primitive data type in Javascript that is used to store large collection of data of different data types in key-value pairs. These key value pairs of objects are called as properties of objects. The values of the keys can be another objects thus making it possible to create complex nested structure.

The syntax to declare object is:

const object_name = {

   key1: value1,

   key2: value2

}

const student = { 
    name: ‘Nikhil’,
    maths: 55, 
    science: 20, 
};
 

// accessing property

console.log(student.name); // Nikhil
console.log(student[“maths”]); // 55
 

 

You can easily access the value of the key in properties using a (.) dot notation or square bracket [] notation.


We will further explore more concepts of Javascript in future blogs!

Till then Happy Coding!😀


Post a Comment

0 Comments