Saved Bookmarks
| 1. |
What are the different data types present in javascript? |
|
Answer» To know the type of a JavaScript variable, we can use the typeof operator. 1. Primitive types String - It represents a series of characters and is written with quotes. A string can be represented using a single or a double quote. Example : var str = "Vivek Singh Bisht"; //using double quotesvar str2 = 'John Doe'; //using single quotes
Example : var x = 3; //without decimalvar y = 3.6; //with decimal
Example : var bigInteger = 234567890123456789012345678901234567890;
Example : var a = 2;var b = 3; var c = 2; (a == b) // returns false (a == c) //returns true
Example : var x; // value of x is undefinedvar y = undefined; // we can also set the value of a variable as undefined
Example : var z = null;
Example : var symbol1 = Symbol('symbol');
typeof 3.14 // Returns "number" typeof true // Returns "boolean" typeof 234567890123456789012345678901234567890n // Returns bigint typeof undefined // Returns "undefined" typeof null // Returns "object" (kind of a bug in JavaScript) typeof Symbol('symbol') // Returns Symbol 2. Non-primitive types
var obj1 = { x: 43, y: "Hello world!", z: function(){ return this.x; } } // Collection of data as an ordered list var array1 = [5, "Hello", true, 4.1]; Note- It is important to remember that any data type that is not a primitive data type, is of Object type in javascript. |
|