Skip to main content

JS - DATA TYPES

JS DATA TYPES


PRIMITIVE               IMMUTABLE

NON PRIMITIVE     MUTABLE

COMPOSITE            COMBINATION OF PRIMITIVE & OTHER DATA TYPES

TRIVIAL                   PRE DEFINED VALUES

 

Java script data types are classified into following three types

1.      PRIMITIVE               NUMBER, STRING, BOOLEAN

2.      TRIVIAL                   NULL, UNDEFINED

3.      COMPOSITE            OBJECT, ARRAY

DATA TYPES


Number 

Integer, Float, NaN , Infinity

Integer Max digit 15; Float Max Digit 17

NAN != NAN;

undefined = = undefined;

null = = null;

String  

Declared with single or double quotation

var a = " i am ok"  // i am ok

var b = "i 'm ok" // i'm ok

var c =  ' "guvi" ' // "guvi"

var d = "i am in \"the\" game"   // i am in "the" game

var e  = "i am in \' the game"    // i am in 'the game

"2">"12" because first digit 2 is greater than 1


STRING METHODS

length

indexof

lastIndexOf (2 methods)


var str = "tree";
var len = str.length ;//4 
str.indexof("e"); //2
str.lastIndexOf("e"); //3
str.lastIndexOf("e",3); //2

toUpperCase /toLowerCase


var str = "tree";
var b = str.toUpperCase(); //TREE
var c = b.toLowerCase();  //tree

replace  - 3 methods:

replace

replace with i -- ignore upper & lower cases;

replace with g -- replaces the word throughout the string(globally)


var str = "Microsoft offics is MICROSOFT product";
var str1 = str.replace("offics","office"// "Microsoft office is MICROSOFT product"
var str2 = str.replace("Microsoft","MS"// "MS office is MICROSOFT product"
var str3 = str.replace(/Microsoft/ig,"MS"// "MS office is MS product"


slice - 3 methods

substring and substr both are different;

substring gives the output simillar to slice

var str  = "1234";
var str1 = str.slice(1); //234    : Method1
var str2 = str.slice(0,2); //12   : Method2
var str3 = str.slice(1,3); //23
var str4 = str.substr(1,3); //234 : Method3

split - 3 methods  -- data conversion

Boolean

True or False

Boolean (0) ('')(null)(undefined)   =  false

Boolean (0.1) ('  ') = true

Undefined

var a; (without any value)

Null

var  a = null (atleast something called null)


UNDEFINED vs. NULL vs. ZERO

In undefined we never assigned any value but  in the case of null at least we assign some value called null.

var a;//undefined

var b = null;

var c=5;

var d = a+c // NaN

var e = b+c  //5

     If  I am trying to do any mathematical  operation with null and number it gives me some  number as output,  instead of null if i am doing it with undefined and number the result is NaN

null !== 0;    undefined !==0;

Array [ ]

Helps to store list of data (it allow mixed data types also).

Index starts from Zero and length starts from the count 1

It consists only numbered index.

var arrv= [1,2,3,4,5]

arr [0] = 1;

arr[5] = 6; //arr = [1,2,3,4,5,6]

arr [7] = 8; //arr = [1,2,3,4,5,6,<empty slot>,8];

arr[7] = 88; // arr =[1,2,3,4,5,6,<empty slot>,88]

length

indexof

lastIndexof

reverse

var arr =[1,2,3,1];
arr.length(); //4
arr.indexOf(1); //0
arr.lastIndexOf(1); //3
arr.reverse//[1,3,2,1]

toUpperCase

toLowerCase

arr = ["a","b","c","D"];
arr[0].toUpperCase(); //"A" for indexes only we can to perform this function not to the whole array
arr[3].toLowercase(); // "d"

PUSH & POP   ---  deal with starting index

SHIFT & UNSHIFT  --- deal with ending index

var arr =[1,2,3];
arr.push(4)  ; //arr = [1,2,3,4]
arr.pop(); //arr = [1,2,3]
arr.unshift(4);//arr = [1,2,3,4]
arr.shift(4); // arr = [1,2,3]

MERGING OF TWO ARRAY / CONCAT

arr  = [1,2,3];
arr2 = [4,5,6];
arr3 = [7,8,9];
arr + arr1 // 1,2,34,5,6 - String
arr + arr1 + arr2 // 1,2,34,5,67,8,9 - String
arr.concat(arr2// [1,2,3,4,5,6]
arr.concat(arr2,arr3// [1,2,3,4,5,6,7,8,9]

Delete and insert @ required index using splice

slice

splice -- helps to insert or delete elements at required index.

delete

arr = ["a","b","c","d"]
arr.slice(1//["b","c","d"]
arr.slice(0,3//["a","b","c"]
arr.splice(1// ["a"]
arr.splice(2,1// ["a","b","c","d"]
arr.splice(2,0,"abc")  //["a","b","abc""c","d"]
delete arr[0// [<emptyslot>,"b","c","d"] // create emptyslot

sort

arr = ["abc","bat","cat","ball","ant"];
arr.sort(); // alphabetical sort
arr = [1,2,4,6,7]
arr.sort(function(a,b){return a-b}) //Numerical sort



Object {  }

Named Index array (instead of [ ] declared inside {  }) 

var  obj = {"name :"Hari" , "age" : "25"}

obj.name // Hari

obj["name"] // Hari

obj.xyz // undefined

obj.xyz.xyz // Error  (undefined.xyz)




obj.keys.length

 


typeof

Number

String 

Boolean

Undefined

Function

Object  --> Object, Array, Null

*Note : Always typeof typeof (anything) is string

typeof typeof()    ===> typeof (" ")  ===> string

 

TASK

 

typeof(1)

typeof(1.1)

typeof("1.1")

typeof("JS")

typeof(true)

typeof(null)

typeof(undefined)

typeof([])

typeof({})

typeof(NaN)


 


 

Comments

Popular posts from this blog

  You are provided with the radius of a circle "A". Find the length of its circumference. Note: In case the output is coming in decimal, roundoff to 2nd decimal place. In case the input is a negative number, print "Error". function   coc () { var   r = document . getElementById ( "num1" ). value ; var   pi = 3.1415 ; var   Circumference =( 2 * pi * r ); if  ( r >= 0 ) console . log ( Circumference ); else console . log ( "Error" )} 
The area of an equilateral triangle is ¼(√3a 2 ) where "a" represents a side of the triangle. You are provided with the side "a". Find the area of the equilateral triangle. function   sqrt () { var   a = document . getElementById ( "a" ). value ; b =  Math . sqrt ( 3 ); c =( 1 / 4 )* b * a * a ; console . log (  "Area =" + c )}

HOISTING

  HOISTING Variables” and “ function” declarations are moved to the top of their scope before code execution. Variables are hosted and not their values Only the normal function will be hoisted not the anonymous,IIFE and Arrow Function will be hoisted Properties are not hoisted PROPERTY  Any variable declared globally  become property of window variable declared without keyword variable declared with keyword var except function scope var console . log ( a )   // Error a not defined console . log ( a );  // undefined --> var a is hoisted not value var   a  =  20 ; console . log ( a , b )  // Error --->property b can't hoist var   a  =  20 ; b  =  40 ; var   a  = add ( 2 , 5 )  // Error console . log ( a ) var   a  = add ( 2 , 5 )  // 7 function add hoist console . ...