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
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)
REST PARAMETER
Rest Parameter helps to store extra argument of functions in array
PARAMETER VS ARGUMENTS
Values passed during function declaration is called as parameter
Values passed during function invoke is called as arguments
ARRAY DESTRUCTURING
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
With Default Values
function output as array
Rest Parameter
OBJECT DESTRUCTURING
keyword var,let,const is important
Undefined Value
Default Value
Assigning new variable name
NESTED ARRAY & NESTED OBJECT DESTRUCTURING
if suppose same index name present to two or more place then assigning to new variable used at destructuring portion
Comments
Post a Comment