Javascript Es6
Javascript Es6
Var variable is function scope Let variable is block scope Const variable is block scope
Var a = 100; { let a =100} { const a =100}
{}
Hoisting done with initializing Hoisting done but not Hoisting done but not
default value initialized initialized
Temporal dead zone
4. Diff b/w ES5 and ES6
ES5 ES6
Function and return keywords used Arrow function new feature dont need function
keyword
Iterating over the values of array Iterating over the properties of an object.
Used in array,string For in does not print
Syntax: let arr= [1,3,5,6,7] Used in object
for(let p in arr){console.log(p)} Syntax: let obj = {a: 1, b: 2, c:3}
Let arr1=[] for(let p in obj){console.log(p, obj[p]);}
Arr1[0] = 20, arr[99] =100 for(let p in arr){console.log(p)} // doesnot print
for(let p of arr){console.log(p)} //1to98 empty
undef
6. Conversion and coercion
Conversion Coercion
Changing value from one type to other Automatically converting value fron one type to
other during operation
7. Rest and spread operator
Rest Spread
Creates new array based on conditions Modifies original array by adding removing
Rest of the items are stored in array Add, delete
Passing arg
8. Diff b/w Splice and Slice
Slice Splice
Creates new array by extracting the Modifies original array by adding removing
portion of array w/o modify original array
Eg: slice(0,2) -> o/p: (0,1) 2 not prints
10. Filter
Filter method used to create new array by satisfying the conditions
Syntax
array.filter((item,index,array) => condition)
11. Reduce
Reduce method is used to transform array into single value, iterates each and every element.
Apply callback function and provide result.
Syntax
array.reduce((accumulator,current,index, array), initialvalue)
Loop through each element of an array Loop through each element of an array
Map method creates a new array Foreach method does not returns a new array just
return the output
Find the 1st element of array if no Create new array and list all element condition
element undefined satisfies empty array returns
14. Diff b/w index and indexOf
index indexOf
Find the 1st element of array if no Create new array and list all element condition
element undefined satisfies empty array returns
15. Includes
Includes method used to check the array includes a particular item it returns a boolean value item is
present or not.
Syntax
array.includes(searchitem, fromindex)
Searchitem - search in the array
Fromindex - index to start the search [optional]
Example
1. Map Example
Syntax: map((item,index,arr) => )
1. let fruits = ['apple', 'banana', 'orange']; //o/p:(3) ['apple', 'banana', 'orange']
let out = fruits.map((item,index) => item)
const frt = fruit.map((item,index) => item.toUpperCase())
console.log(frt); o/p: ['APPLE', 'BANANA', 'ORANGE']
2. let arr = [{name:"ramya",age:"25"},{name:"ram",age:"28"},{name:"reona",age:"1"}]
let out = arr.map((item,index) => ({...item,status:item.age>=20?"added":"removed",}))
console.log(out);
o/p: (3) [{…}, {…}, {…}]0: {name: 'ramya', age: '25', status: 'added'}
1: {name: 'ram', age: '28', status: 'added'}2: {name: 'reona', age: '1', status:
'removed'}
3. let out = fruit.map(function(item,index,arr){
return{
item: item,
length: item.length,
position: index,
total: arr.length}})
console.log(out);
4. Merging two array
let fruit = ["apple", "orange"] ,let color = ["red", "orange"]
o/p (2) ['apple-red', 'orange-orange']
let out = fruit.map((item,index) => (item + "-" + color[index])) console.log(out);
5. let arr = [1,2,3,4,5]
let dbnum = arr.map((item,index) => item * 2) console.log(dbnum); o/p [2,4,6,8,10]
let dbnum = arr.map((item,index) => item % 2 ==0 ) console.log(dbnum);
o/p: [false, true, false, true, false]
2. Filter Example
Syntax: filter((item,index,arr) => )
1. let fruits = ['apple', 'banana', 'orange']; //o/p:(2) [‘'banana', 'orange']
let out = fruits.filter((item,index) => item.length > 5 )
let out = fruits.filter((item,index) => item.startsWith("b")) o/p [banana]
2. let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; o/p (10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let out = numbers.filter((num) => num <= 10)
let out = numbers.filter((num) => num % 2 == 0) o/p (5) [2, 4, 6, 8, 10]
3. let user = [{name:"ramya", age: 25},{name:"raj", age: "29"},{name:"reona", age: 1}]
let out = user.filter((item,index) => item.age>1) o/p (2) [..]
let out = user.filter((item,index) => item.name.length>=3) o/p (3) [...]
console.log(out);
4. let items = [{name:"apple", category: "fruit"},{name:"brinjal", category:
"veg"},{name:"banana",category:"fruit"}] o/p (2) [..]
let out = items.filter((item,index) => item.category === "fruit")
console.log(out);
5. let books=[
{ title: 'Eloquent JavaScript', author: 'Marijn Haverbeke', year: 2011 },
{ title: 'JavaScript: The Good Parts', author: 'Douglas Crockford', year: 2008 },
{ title: 'Learning Web Design: A Beginner\'s Guide to HTML, CSS, JavaScript, and
Web Graphics', author: 'Jennifer Niederst Robbins', year: 2012 },
{ title: 'HTML and CSS: Design and Build Websites', author: 'Jon Duckett', year: 2011
},
{ title: 'CSS Secrets: Better Solutions to Everyday Web Design Problems', author:
'Lea Verou', year: 2015 },
{ title: 'JavaScript and JQuery: Interactive Front-End Web Development', author: 'Jon
Duckett', year: 2014 },
{ title: 'You Don\'t Know JS', author: 'Kyle Simpson', year: '2014-2019' },
{ title: 'React: Up & Running', author: 'Stoyan Stefanov', year: 2016 },
{ title: 'Node.js Design Patterns', author: 'Mario Casciaro', year: 2014 },
{ title: 'Head First Design Patterns', author: 'Eric Freeman and Elisabeth Robson',
year: 2004 }];
8. Concat Example
Syntax: a.concat(b)
let a= [1,2,3,4] ; let b =[5,6,7,8,9] let c=[10,13,14]
1. let out = a.concat(b,c); o/p: (12) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14]
2. let out = a.concat(b,c,20,30,40); o/p: ‘’ ..10, 13, 14, 20, 30, 40]
9. Sort Example
Syntax: splice(start,length,new element)
let a= [99,81,2,45,100]
1. let out = num.sort((a,b) => {return a-b}) o/p: (5) [2, 45, 81, 99, 100]
2. let out = num.sort((a,b) => {return b-a}) o/p: (5) [100, 99, 81, 45, 2]
const names = [“kumar”, “arun”, “joes”, “ramya”]
1. let out = names .sort() o/p: (4) ['arun', 'joes', 'kumar', 'ramya']
2. let out = books.sort((a,b) => {return a.year - b.year})
10. Fill Example
Syntax: splice(value,start,end)
let num = [1,10,1,10,22,4,6,9];
1. let out = num.fill(500); o/p: (8) [500, 500, 500, 500, 500, 500, 500, 500]
2. let out = num.fill(500,4);o/p: (8) [1, 10, 1, 10, 500, 500, 500, 500]
3. let out = num.fill(500,4,6);o/p: (8) [1, 10, 1, 10, 500, 500, 6, 9]
11. Join Example
let sta = ["apple","bananan","orange","apple"]
1. let out = sta.join() o/p: apple,bananan,orange,apple
12. Push Example
let fruit1 = ["apple","bananan","orange"]
let fruit2 = ["apple","banana","orange"]
1. let out = fruit1.push(...fruit2); console.log(fruit1); o/p: (6) ['apple', 'bananan', 'orange',
'apple', 'banana', 'orange']
13. Object Clone
Const a ={a:1,b:2,c:3},Const b ={c:1,d:2,e:3}
Const c = Object.assign({}, a,b);