Javascript–Variables and Data Types

Javascript is a Dynamically Typed language meaning the Interpreter assigns variables a type at runtime based on the type of data in the variable at that time. Javascript is one of the dynamically typed or untyped languages.

How do we declare the variable in Javascript. Very simple!! No need to mention the type of variable.

Example:

//string

let studentName = “Raghu”;

There are 8 basic data types in Javascript.

Seven primitive data types:

  1. number – numbers of any kind (integers or floating point)
  2. Bigint – for integer numbers of arbitrary length
  3. string – for strings
  4. boolean – for true/false
  5. null – for unknown values
  6. undefined – for unassigned values
  7. symbol – for unique identifiers

Non-primitive data type:

  1. Object – for more complex data structures

In the below example, let us see how the variables for the above data types are declared and used:

alldatatypes.js

					

//Below are the primitive data types

//string
let studentName = "Raghu";
//number
let studentId = 215;
let passPercentage = 89.85;
//boolean
let isPass = true; 
//BigInt
let bigInt = 1234567890123456789012345678901234567890n;
//null
let address = null;
//Undefined
let marks;
//Symbol
let symbolId = Symbol("id");

console.log("Data type of -- studentName is: "+typeof(studentName));
console.log("Data type of -- studentId is: "+typeof(studentId));
console.log("Data type of -- passPercentage is: "+typeof(passPercentage));
console.log("Data type of -- isPass is: "+typeof(isPass));
console.log("Data type of -- bigInt is: "+typeof(bigInt));
console.log("Data type of -- address is: "+typeof(address));
console.log("Data type of -- marks is: "+typeof(marks));
console.log("Data type of -- symbolId is: "+typeof(symbolId));

Index.html

					
<html>
    <head>
    </head>
    <body>
    </body>
    <script type=”text/javascript” src=”alldatatypes.js”>
    </script>
</html>

Console Output

datatyperesponseconsole

In the first screenshot which is of the Javascript file. We can see for every data type, variable declaration is same. I have just used the “let” keyword. Simple right??

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.