JS Tutorial

Function Reference

Method Reference

  • JS String
  • JS Array
  • JavaScript Data Types


    Data Types in JavaScript

    Data types to define what types of data can store values and changed it.

    There are following types of data types and strucutre

    • Primitive data types
      • boolean
      • number
      • string
      • bigint
      • symbol
    • composite data types
      • Object
      • Array
      • Function
    • special data types
      • undefined
      • null

    Data Types Example in JS

    Some examples are primary data types in JavaScript

    var age = 18;			// Number
    var name = "Amit Kumar";	// String
    var obj = {name:"Raj Kumar"};	// Object
    

    Dynamic Data Types in JavaScript

    JavaScript is a loosely typed and dynamic language. JavaScript variables not correlated data value so It can change

    var a;					//a is undefined
    var a = 18;				//a is Number
    var a = "Amit Kumar";	//a is String
    

    Example

    /*
    In this example 16 is treated as string like "Apple" + "16"
    */
    var a = "Apple" + 16;
    // Output : Apple16
    
    var a ="3" + 16;
    // Output : 316
    
    In JS when addition of number and string if any value in string, JS will treat number as a string.

    Example

    var a = "16" -3;
    // Output : 13
    
    var a = "16" - "3";
    // Output : 13
    

    JavaScript typeof Operator

    typeof operator used to check the type of variable. This can be used with or without parentheses like (typeof(a)) or (typeof a).

    Example

    //Strings
    typeof '';		// Returns "string"
    typeof "";		// Returns "string"
    typeof 'ABCD';		// Returns "string"
    typeof '456';		// Returns "string"
    typeof "456";		// Returns "string"
    
    //Numbers
    typeof 0		// Returns "number"
    typeof 456		// Returns "number"
    typeof 45.3		// Returns "number"
    typeof (45+6)		// Returns "number"
    typeof (456)		// Returns "number"
    typeof 3.5e-2		// Returns "number"
    typeof Infinity		// Returns "number"
    typeof NaN		// Returns "number"
    
    // Booleans
    typeof true;		// Returns "boolean"
    typeof false;		// Returns "boolean"
    
    // Undefined
    typeof undefined;		// Returns "undefined"
    typeof undeclared_variable;	// Returns "undefined"
    
    // Null
    typeof null;		// Returns "object"
    
    // Objects
    typeof {};		// Returns "object"
    typeof {name: "ABC"};	// Returns "object"
    
    // Arrays
    typeof [];  		// Returns "object"
    typeof [4,5,6]; 	 // Returns "object"
    
    // Functions
    console.log(typeof isNaN);	// Returns "function"
    typeof function(){};		// Returns "function"
    

    JavaScript Strings Data Types

    A String is set of characters like "Vipin Kumar", it is denoted by single quotes '' or "" double quotes

    Example

    var name1 = 'Vipin Kumar';	// using single quotes
    var name2 = "Vipin Katiyar"; 	// using double quotes
    

    We can use single quotes inside the string

    Example

    var m = "It's fine"; // single quote inside double quotes
    var n = 'It\'s fine'; // escaping single quote with backslash
    var p = 'I am fine and "You"';  // double quotes inside single quotes
    var q = "I am fine and 'You'";     // single quotes inside double quotes
    

    JavaScript Numbers Data Type

    A Numbers data type is number with or without decimals, positive or negative, or numbers written using exponential notation e.g. 1.8e-4 (0.00018)

    Example

    typeof 0		// Returns "number"
    typeof -456		// Returns "number"
    typeof 456		// Returns "number"
    typeof 45.3		// Returns "number"
    typeof (45+6)		// Returns "number"
    typeof (456)		// Returns "number"
    typeof 3.5e-2;		// Returns "number"
    typeof Infinity;	// Returns "number"
    typeof NaN;		// Returns "number"
    

    Example : Infinity

    console.log(456 / 0);	//Infinity
    console.log(-456 / 0);	//-Infinity
    console.log(456 / -0);	//-Infinity
    console.log(-456 / -0);	//Infinity
    

    Example : NaN

    console.log('test' / 3); // NaN
    console.log('test' / 3+4); //NaN
    

    JavaScript Boolean Data Type