Skip to main content

JS DATA TYPES

 

               JAVASCRIPT DATA TYPES

 

 

1.      NUMBER

2.      STRING

3.      BOOLEAN

4.      NULL

5.      UNDEFINED

6.      ARRAY

7.      OBJECT

 

 

NUMBER

int,float

STRING

Everything inside the quote are String

‘word’

“Words” 

‘5’

How to write Quotes Inside String Data Type

I ‘m  here

“ Happy Learning “

 

METHOD 1 –  Opposite Quotation

“I ‘m  here “

‘ “ Happy Learning “ ‘

 

METHOD 2 –  Backslash

‘ I \’m  here ‘

‘\“ Happy Learning \“ ‘

This method is suitable for text editor only ( not for console)

 

BOOLEAN

true/false

Boolean ()

null,undefined ,empty string  = false;

0        =  false;

  “0”  =  true;

 

 

NULL vs. UNDEFINED

var a ;      (undefined  - nothing) 

var b = null; (null – Something) 

var c=5;

var d = a+c // NaN

var e = b+c  //5

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

Doing mathematical operations alone null value will be consider as ZERO but null is not equal to ZERO

 


 

ARRAY /OBJECT

Both are mixed data types

Array – [] Square Bracket– with number index

Object – {} Curly Bracket – with number, string index – Key Value Pair (entries)

 


 

PRIMITIVE  -- immutable  -- Number , String ,Boolean

TRIVIAL    - already have some values(predefined values) -  null,undefined

COMPOSITE  --  consist two or more mixed data types  -  Array, Object


 

typeof operator

 

Number,String,Boolean,Undefined

Null,Array,Object  -- Object

typeof(typeof()) = String

 

JS OPERATORS

 

 MATHEMATICAL OPERATOR

1. Addition  +

2. Subtraction   -

3. Multiplication / Exponentiation *  / **

4. Division / Modulus   -----   /    %

5. Concatenation -- Adding Strings or Number and string

 

INCREMENT & DECREMENT OPERATOR

A++   ;  ++ A

A - - ;  --A

ASSIGNMENT OPERATOR

a=+b 

Which means a = a+b

LOGICAL OPERATOR

AND &&

OR ||

NOT !

 

TERNARY OPERATOR

 Condition ?  True : False;

If that condition is true left side of semicolon will get execute otherwise right side will get execute

x>5 ?  console.log( X is higher than 5)  : console.log( X is lesser than 5)

 

 

Note : Maximum Ternary and Logical Operators are used in  if Condition Statement

 

COMPARISON OPERATOR

 

Double digit operator – only values and not data types

Triple digit Operator /Strict Operator – Values and data types

 

Array Comparison – Stringify

JSON.stringify(arr1) = = = JSON.stringify(arr2)

 

Object Comparison 

 


<script>
    var obj1 = {
        name: 'susi',
        age: 10
    }
    var obj2 = {
        age: 10,
        name: 'susi'
    }

    function objcomp(obj1obj2) {

        var arr1 = Object.keys(obj1);
        var arr2 = Object.keys(obj2);
        var count = 0;
        if (arr1.length === arr2.length)
         {
            for (i = 0i < arr1.lengthi++) {
                if (obj1[arr1[i]] == obj2[arr1[i]])
                    count++;
            }
        }

        if (count == arr1.length)
         {
            console.log(true)

        } 
        else {
            console.log(false)
        }


    }

    objcomp(obj1obj2)
 
</script>

 

 

DATA TYPE CONVERSION

 

NUMBER TO STRING

var  a = 10

String(a)

 

STRING TO NUMBER

var  a = ‘10’

Number(a);

parseInt(a);

parseInt(“10.4”) or parseInt(“10.8”) = 10

parseFloat(a);

var b = ‘happy’

Number(b);  =  NAN

 

 

STRING TO ARRAY

var a = ‘happy  ma’

split – 3 methods

Without Space

a.split(‘’) = [‘h’,’a’,’p’,’p’,’y’,’ ‘,’m’,’a’]

With Space /Comma

a.split(‘  ’) =[‘happy’,’ma’]

With Limit

a.split(‘  ’,1) =[‘happy’]

ARRAY TO STRING

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

join – 3 methods

Without Comma

a.join(‘’) = ‘12345’

With Comma

a.join(‘,’) = ‘1,2,3,4,5’

With Space

a.join(‘   ‘) = ‘1 2 3 4 5’

 

ARRAY TO OBJECT

var a = ['a','b','c','d']

var b =Object.assign({},a)  = object { 0: "a", 1: "b", 2: "c", 3: "d" }

 

OBJECT TO ARRAY   

var a = { 0: "a", 1: "b", 2: "c", 3: "d" }

Object.keys(a)  = [0,1,2,3]

Object.values(a)  = [‘a’,’b’,’c’,’d’]

Object.entries(a)  = [[0,’a’], [0,’b’], [0,’c’], [0,’d’]]

 

COPY BY VALUE vs COPY BY REFERENCE

Copy By Value -- Copy of the value will be passed to the new variable, formal argument and actual argument are stored in different memory location(address)

Copy By Reference -- Value itself passed to the new variable, formal argument and actual argument are stored in same memory location (address)

 

Normally Objects use Copy by reference method.

Call by /copy by / pass by are different methods

 

SPREAD OPERATOR

By using Spread Operator we can avoid storing formal and actual argument on the same memory location (address)

 

 <script>
    var arr1 = [123];

    var arr2 = [...arr1//copying with the help of spread operator

    var obj1 = {
        name: 'priya',
        age: 24
    }

    var obj2 = {
        ...obj1
    }

//  helps to combine array and object

var arr1 = [1,2,3];
var arr2 = [...arr1,4,5,6];
var arr3 = [0,...arr2]

var obj1 ={name:'priya',age:24};
var obj2 ={city:'Trichy'state:'TN'};
var obj3 = {...obj1,...obj2}


</script>

 

 

 

REST PARAMETER

Rest Parameter helps to store extra argument of functions in array

<script>

  // REST PARAMETER

  function add(a,b,...arg){
      console.log(a+b)
  }

  add(4,7,9,2,3,6)  //a =4,b=7, arg =[9,2,3,6]


</script>

 

  

PARAMETER  VS ARGUMENTS

Values passed during function declaration is called as parameter

Values passed during function invoke is called as arguments

 

ARRAY DESTRUCTURING

 

<script>
    var arr = [123];
    //[a,b,c] = [1,2,3]
    var [abc] = arr//a =1, b=2 , c=3
  </script>

 

 

Not compulsory to use keyword var,let,const

We cant use numbers for array destructuring

[1,2,3] = arr (wrong)

 

With extra and less values

 

 
<script>
    [abcd] = arr // d=undefined

      [a, , b] = arr // a=1 ,b =3
 
 </script>

 

With Default Values

   
 [a = 30bc = 10] = [102030// a=10;b=20;c=30

//only the undefined default value will be replaced

[a = 30bc = 10] = [1020undefined// a=10;b=20;c=10

[a = 30bc = 10] = [null20undefined// a=null;b=20;c=10


 

function output as array

 

 
function print(arr) {

return (arr)
}

var [abc] = print([123])

 

Rest Parameter

 
var arr = [1,2,3,4,5,6,7,8,9,0]
  
  [a,b,...c] = [1,2,3,4,5,6,7,8]; // a =1,b=2,c=[3,4,5,6,7,8]

  [a1,b1] = c // a1 = 3,b1=4
 

 

 

OBJECT DESTRUCTURING

 

keyword var,let,const is important

 

 
var obj = {name:'priya',age:24}
        
        var {name,age} = obj//name =priya age=24

 

 

Undefined Value

 
var {name,age,city} = obj//name =priya age=24 city=undefined

var{name,city,age}= obj//name =priya age=24 city=undefined

 

 Default Value

                var {name='sasi',age,city='trichy'} = obj;
         
         //only the undefined default value will be replaced

 

 Assigning new variable name


var obj = {name:'priya',age:24}

var {name:x1,age:n1} = obj ; //x1=priya , n1 =24



 

NESTED ARRAY & NESTED OBJECT DESTRUCTURING

// NESTED ARRAY DESTRUCTURING

var arr = [1,2,['app','basics','computer'],3,4];

[a1,a2,[s1,s2],,a4] =arr

[a1,[s1,s2],,a4] =arr//error

[a1,,[s1,s2],,a4] =arr

 

 

  //NESTED OBJECT DESTRUCTURING

var student = {
    name: 'John Doe',
    age: 16,
    scores: {
        name: 'Unit Test',
        maths: 74,
        english: 63
    }
};

var{name,age,} = student;

var{name,age,scores:{maths,tamil=80}} = student
 

 

 

if suppose same index name present to two or more place then assigning to new variable used  at destructuring portion

 
var {name,age,scores:{name:studentname,maths,science=30}} = student;

//name - john
//studentname --unit test
 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

CODEKATA BITWISE

Given a number N and an array of N elements, find the Bitwise XOR of the array elements. Input Size : N <= 100000 Sample Testcase : INPUT 2 2 4 OUTPUT 6   // ONE TESTCASE PASSED const   readline  =  require ( "readline" ); const   inp  =  readline . createInterface ({    input :   process . stdin }); const   userInput  = []; inp . on ( "line" , ( data )  =>  {    userInput . push ( data ); }); inp . on ( "close" , ()  =>  {    var   N  =  Number ( userInput [ 0 ]);    var   str  = String ( userInput [ 1 ])    var   arr  =  str . split ( ' ' ). map (( val ) => Number ( val ))       var   first  =  arr [ 0 ]       for ( i = 1 ; i < N ; i ++)   {  first  = first ^ arr [ i ]}    console . log ( first ...

DATA BASE

  DATA BASE Backend deals with Server and Data Base CLOUD COMPUTING Cloud – Cloud Computing -- Rented Computer Service Ram, Processor, OS, Storage WHY WE PREFER CLOUD COMPUTING 1. Capital Investment (at initial stage renting is better than purchasing) 2. Maintenance Problem (Power Backup, Hard Disk, Upgrade) 3. Easily Swappable (Scale Up & Scale Down) (festival time ecommerce need extra storage and facilities after that it may need less, College websites during exam time (E.g)   Anna university website ,recent time – income tax website – pan card & aadhar card linking) 4.   Don’t need any separate place to keep those Systems. TOP COMPANIES AWS – Amazon Web Service - Netflix GCP – Google Cloud Platform Azure - Microsoft Azure WHY LINUX PREFERRED THAN OTHER OS Linux, Windows, Mac 1. Open Source 2.   Secure than Windows & Mac. 3. Command Line Usage In the Linux OS, the command line is a very handy and powerful tool used for...

DAY 15 - AUG 08

  DOM – DOCUMENT OBJECT MODE   HTML DOC –Browser – Browser Engine(html parser) – DOM   Tree structure takes less time for searching that’s why we prefer tree structure in DOM. Why we prefer object model? Object model means OOPS concept based it have features like data abstraction, encapsulation…. Window   -- Window is the default thing available in the Browser’s JavaScript Engine. Document -- Document is property of Window Screen – Physical dimension of   the browser   Draw some sampl tree We can create html with the help of tag and make it dynamic(to do some action) by adding   javascript   using script tag then why specifically we need this dom à Will understand in future classes that we can’t do something directly with the tag.   DOM MANIPULATION   var element =document.createElement(‘tag’) Element.setAttribute(“attribute”,”name”) If we have morethan one same attribute for the element we can’t use s...