Javascript Data Types

javascript data types

Datatypes in javascript means the type of data stored in a variable.

As Javascript is loosely typed scripting language, there is no type cast in javascript. We can create any type of data using a single variable i.e. var.

var means a variable which can store any type of data. Data type of variable is not declared.

Declaring var means creating a new variable in memory with variable name after white-space. Assignment Operator (=) means assigning value to variable declared.

We can also use const and let to declared variables.

Datatypes in JavaScript

  1. Primitive datatypes
  2. Refrence datatypes

Primitive datatypes in JavaScript

Primitive datatypes are the basic or common data types in javascript. Like string, numbers, boolean, undefined and null. They are very commonly used data types.

var is used to decalare primitive datatypes in javascript.

Primitive Data Type Meaning
var x; undefined
var x=undefined; undefined
var x=null; null type data
var x=3; Data Type is number.
var x=3.5 Data Type is number with decimal
var x="3" Data Type is string
var x='3' Data Type is string
var x="HELLO" Data Type is string
var x=true Boolean data type
var x=false; Boolean data type

Strings

Anything written in single and double quotes are strings in javascripts. Strings are used to store name, email, city name, password etc in javascript. JavaScript Strings.


    var name="js string";
        

Numbers

JavaScript Numbers are used to perform Arithmatic Operations (+,-,*,/,%). Numbers are written without quotes. JavaScript Numbers.


    var num=20;
        

Boolean

JavaScript Boolean are true and false. Booleans are used in conditions, comparision etc.


    var t=true;
    var f=false;
        

Undefined

JavaScript Undefined means any variable whose value is not assigned yet. Anything variable whose value is not assigned is undefined.


    var u;
    var t=undefined;   
        

Null

JavaScript null is a special object with empty value. null is used where value is defined, but still it is not there. It is also used in exceptionl handling.


    var u=null;
        

Reference Type in JAVASCRIPT

Reference are datatypes based on primitive. Like Array, Object and Functions. Everything is JavaScript is either a primitive datatype or Object. Even Arrays and Functions are objects, but they are build-in objects.

var is also used to declare reference datatypes.

Reference Data Type Meaning
var x=[ "Jan", "Feb", "Mar" ] Array
var x={ "name" : "ABC", "age" : "22", "gender" : "male" } Object
var x=function(x,y){ return x+y;} Function Expression
function sum(x,y){ return x+y;} Function Declaration
var x=new Date(); Date
var x=/^[0-9]{6}$/ Regex

typeof Operator

typeof operator in javascript is used to check data type of a variable. It can return string, number, boolean and undefined. For reference type and null, typeof operator will return object.


var x;                 // undefined
var y=9;               // number
var z="Tech Altum";    // string
					
typeof(x) and typeof x will return undefined,
typeof(y) and typeof y will return number,
typeof(z) and typeof z will return string.
Given Expression Check Data Type
var x;
var x=undefined;
var x=null;
var x=3;
var x=3.5;
var x="3";
var x='3';
var x="HELLO";
var x=true;
var x=false;
var x=function(a,b){ return a+b};
function add(a,b){ return a+b}
  1. typeof operator can check datatype of strings, numbers, undefined, boolean and function.
  2. For Arrays, & Objects, typeof operator will return object.
  3. typeof(null) will return Object.