Datatypes in JavaScript?

What are Datatypes in a programming language?

Datatypes define the type of data the variable can store. Based on these datatypes a variable can perform certain operations which other datatypes might not perform.

Examples of datatypes: String, integer, Boolean, etc.

How do you declare a variable with a certain datatype in JavaScript?

Since JavaScript is a dynamically-typed language so you cannot specifically declare the type of the variable.

Dynamically-typed languages are those (like JavaScript) where the interpreter assigns variables a type at runtime based on the variable's value at the time.

So you just declare a variable with let or const or var and based on the value of that variable JavaScript will automatically assign the type to the variable.

Datatypes in JavaScript?

The following datatypes are present in JavaScript:

1. String- stores a string value. You add value inside either double quotes or single quotes or backtick(backtick is present below Esc button).

let s="string data type stores string value,in double quotes";
let s1='string data type stores string value,in single quotes';
let s2=`string data type stores string value,in backticks`;
  1. Number- stores integer value.

     let x=20;
    
  2. Boolean- stores either true value or false value. Generally used for checking a condition.

     let bool=true;
     let bool2=false;
    
  3. Undefined- In JavaScript, a variable without a value, has the value undefined. The type is also undefined. Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

    Don't confuse an empty string with an undefined, both are different. An empty string is still a string.

     let car;//stores undefined 
     let car1 = "Volvo";
     car1 = undefined;//previously car1 was storing a string, now it is empty;
    
  4. Array- it is a collection. It can be a collection of strings, numbers, or both, or can be a collection of array itself. Generally, arrays will be meaningful collections like collections of phoneNumbers etc.

    JavaScript arrays are written with square brackets.

    Array items are separated by commas.

     let countries=["India","USA","Russia"];
     let mix=["India",1,true];//contains a string, a number, a boolean value
    

There are other datatypes as well like Bigint, Null, Symbol, and Object, the most common ones that are widely used except for Object are explained.Object is a new topic in itself and very important, will be covered in upcoming blogs.

Note: The datatype of a variable can switch from string to number, from number to boolean and so on, in JavaScript.

let x=10;//here the type of x is number
x="India";// but here it became a sting.

The typeof Operator

You can use the JavaScript typeof operator to find the type of a JavaScript variable.

The typeof operator returns the type of a variable or an expression.

x=10;
console.log(typeof x);//returns Number

Thank you for reading. Have a nice day ahead.

Did you find this article valuable?

Support Manish Singh Bisht by becoming a sponsor. Any amount is appreciated!