Javascript Datatypes

Javascript Datatypes

In this blog, we are going to explore Datatypes in Javascript.

Anusha V's photo
Anusha V
·May 3, 2022·

3 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

What is Datatype?

Datatype concept is more important in Programming Languages. Because, while defining the variable - What type of Data is stored in the variable is decided by the Datatype, and based on that it will perform the operation or expression given on it.

In other programming languages, the Datatype for the variable is decided before assigning a value to that variable and the data type of that variable can’t be reassigned with another datatype value later(i.e., Once a Datatype is fixed for a variable and we can’t reassign the value to it).

But, In Javascript, Datatype is not fixed(i.e., Till we assign a value to a variable, its Datatype is not fixed) and Datatype can be varied anytime. Therefore, Javascript types are dynamic.

Let us understand by example,

In C Language:-

int d = 3; //Variable defining

In the above example, while defining a variable itself we are giving which datatype value it should store in that variable. If you try to store the float value, it shows an error because it can store only integer values.

In Javascript:-

let a = 5; //Storing integer value
let b = ”apple”; // storing string value
b =  8; // Reassigning the value and datatype is varying.

In the above example, till we assign a value to that variable its datatype is not fixed and we can store any data value in that variable and after defining we can reassign another data value in the same variable. So, Javascript has a variant datatype.

Datatypes in Javascript:-

Datatypes in Javascript are,

  • String
  • Number
  • Boolean
  • Objects
  • Functions

String:-

  • A String is a sequence of characters.

  • The string is enclosed within single or double quotes.

  • We can also specify Single-quotes within the double quotes and vice-versa.

Example:-

let  str  =  ”Characters”;  //String Datatype

let  str1  =  ‘Sequence of “Characters” ‘;

Number:-

The number data type contains decimal and non-decimal values.

Example:-

let  a  =  35;  // without decimal

let  b =  12.5; // with decimal

Array:-

The array is a sequence of variables stored under a single variable within Square Brackets [ ], separated by a comma.

Example:-

let  a  =  [1,2,3,4,5];  // Array

Boolean:-

Boolean datatype has two values True or False.

Example:-

let  x = True; 
Let y = False; // Boolean values

Objects:-

With objects, we can store multiple values within the same variable in the form of name and value pairs.

Example:-

let obj = {
       firstname:"Anusha”,
       lastname:"Viswanathan”
}; // objects

Function:-

You might wonder! for functions, we have a datatype. Yes, because we can declare the function and store it inside a variable.

Example:-

let func=function(){
//function code goes here
};

Note:- We can explore functions in-detail in future blogs.

typeof Operator:-

typeof operator is used in finding the variable, that belongs to which data type.

Let us understand by example,

let str1 = “characters”;
console.log(typeof  str1); //String
let  a  =  35; 
console.log(typeof  a); // Number
let  a  =  [1,2,3,4,5];
console.log(typeof  a);  /*Object(Because array is a special kind of object, so when we output it it will print as object*/
let  x = True; 
console.log(typeof  x); //Boolean
let obj = {
       firstname:"Anusha”,
       lastname:"Viswanathan”
};
console.log(typeof  obj); //object
let func=function(){
//function code goes here
};
console.log(typeof  func); //function

In the above example, with the typeof operator, we can identify the datatype of a variable.

In this blog, we have seen the datatypes in Javascript and we will see in detail about these data types in future blogs.

 
Share this