techStackGuru

Javascript Data types


In JavaScript, there are two sorts of data types -

  • Primitive data type
  • Non-primitive (reference) data type

Primitive data type

Data TypeDescription
NumberIntegers or floating-point
StringSeries of one or more characters
BooleanTrue or False
UndefinedUnassigned values
NullUnknown values

Non-primitive data type

Data TypeDescription
ObjectKey:value pairs
ArrayGroup of similar values

Primitive types

let value = 85;                                     // Number
let name = "Vishal Torgal";                         // String
let bigInt = 56789895678989567898956789895678989;   // Bigint
let booleanValue = ture;                            // Boolean
let value = undefined;                              // Undefined 
let value = null;                                   // Null 

JavaScript types are dynamic. For example

let val;           // Now val is undefined
val = "Torgal";    // Now val is a String
val = 5;           // Now val is a Number 

Non-Primitive types

// key-value pairs collection
var obj = {
    no:  1,
    country:  "India"
}
       
// Ordered list collection   
var arr = [1, true, "India"]; 

Type coercion

The conversion of values between different data types is known as type coercion. Expressions in JavaScript are evaluated from left to right.

// Converted to string
let val = 20 + "aura";
console.log(val); // 20aura

// Before reaching "aura" string, 20 and 5 were considered as number.
let val = 20 + 5 + "aura";
console.log(val); // 25aura

// Converted to string
let val = "hello" + 20 + 5;
console.log(val); // hello205 

Strings can we written with single quote, double quotes and backticks.

// With single quotes:
let val = 'Aura';

// With double quotes:
let val = "Hello"; 

Combining single and double quotes.

let val1 = "Hello 'Aura'";
console.log(val1)  // Hello 'Aura'

let val2 = 'Learning "Javascript"';
console.log(val2) // Learning "Javascript"  

Exponential Notation

let val1 = 99e7;    
console.log(val1) // 990000000

let val2 = 99e-7;   
console.log(val2) // 0.0000099  

typeof operator

It is used to determine the data type of its operand.

typeof 85                             // "number"
typeof "Torgal"                        // "string"
typeof false                           // "boolean"
typeof 4342371323421391137011120n      // "bigint"
typeof NaN                             // "number" 

undefined

Represents values is not defined.

let val;
console.log(val);   // undefined  

null

Represents empty or unknown value. NULL or Null is not same as null.

let val = null;
console.log(val);   // null  

previous-button
next-button