Part 3: two discripency with JS data types
1. null data types. typesOf(null) //object
2. function data type does not exist in JS. tupesOf(function) // function
Note :
1. All function in JS is actually property of an object.
2. object contains two types that is 1. array and 2. function
3. symbol data types is new in JS included with ES6. typeOf() will return the data types of variable.
4. JS array and function are also object data types.
5. Primitive data type refer to a single value in an address in memory. where as non-primitive data type refer to the address in memory which contains single or multiple key value pairs.
Primitive and non-primitive data types:
there are 6 primitive and 1 non-primitive data type. total 7 are here -
1. number, string, boolean, null, undefined, symbol
2. object:-- contains 2: array, function
Note: symbol data type is new in JS included with ES6. typeOf() will return the data types of variable.
scene1:
var a=5; // when we create a variable it create a space for itself in the memory. When we try to assign another value with this variable like a =6; It does n't alter the value. It will create another a=6;
a1=a //assign value in another variable
a= = = a1 // true , It will check value and typeOf()
scene 2:
var obj1 = {a=5,a1=6}
var obj2= obj1 // here we are assigning the address of obj1 to obj2 not value in case of object you have to memorize it.
obj1 = = = obj2 // true because both will refer same address.
scene3:
var obj1 = {a:5,b:6};
var obj2 = {a:5,b:6};
obj1 = = = obj2 // false because both will refer different address. do not forget it.
Note: when we compare two object, javascript compare thier address not value.
Data types value
number integer, float, exponential, NaN, Infinity
var a =2; // integer
var b = 2.5 // float
var c = 10e4 //exponential 10*10 000
var d = "Rohit"; string check typeOf(d) // string
var e = NaN // typeof(NaN) // number
var f = 5/0 ; // infinity typeof(f); // number
typeOf(infinity) // number
typeOf(NaN) // number
var g = Number(5); // number literal
console.log(g);
Note:
we can create number literal by using number function. we can also create number object.
var num1 = new number(6);
console.log(num1);
typeOf(num1);
string ---- group of character inclosed by single or double quotes or backtricks
a= "Rohit" //double quotes
b= 'Raj' // single quotes
c =`Singh` // backtricks
var str4 = string('hi');
// This creates a string literal with the value hi
Note:
1. The string() is also used to convert a non-string value to a string. like this: string(4)
2. Number and boolean data types a string object can be created using the new operator.
var str5 = new string('hello');
console.log(str5); //'hello'
Boolean: Boolean datatypes has only two values. It's mostly used to check logical conditions.
typeOf(true) // boolean
typeOf(false) // boolean
scene1: a=5, b=6, a = = b // false
if(a<b)
alert('a is smaller than b');
var expression = a<b;
we can create the new boolean variable using the Boolean() function. like
var check = Boolean(expression)
var check = Boolean(a<b);
Note: --- Boolean() function can also convert non boolean value to a boolean value;
var myString = 'hi';
Boolean(myString) // true because value exists
Note: Boolean object can be created by using new operator.
area = "2"
var data = new Boolean(area);
Note: we can create object of primitive data type like this?
undefined: // variable is declared but doesn't contain any value;
note: when we try to access the value of variable that is declared only but we forget to assign the value.
array = [1,2,3,4]
array.unshift(8); // used to add the value on index0
array.shift() // used to remove the value on index0
Comments
Post a Comment