0% found this document useful (0 votes)
110 views

Javascript Es6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Javascript Es6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript Es6 Interview Questions

1. Arrow Function and its advantages


Arrow function is the es6 features and has some advance features compared to regular
function. Avoids the usage of bind option.
2. indexOf
Find the index of the first occurrence of a value in array or string. It returns first
occurrence if does not find anything return -1.it checks the first occurence only
3. Diff b/w Var, Let and Const
Var Let Const

Var variable is function scope Let variable is block scope Const variable is block scope
Var a = 100; { let a =100} { const a =100}
{}

Updated and redeclared Updated and cannot Cannot Updated and


Updated a = 100; redeclared redeclared
Redeclared Var a = 300 Updated a = 300 Cannot updated a = 300
cannot redeclared cannot redeclared const a =
let a = 500; already declared 500; already declared

Declared without initialization Declared without initialization Cannot be Declared without


Var a; Let a; initialization
const a; Missing initializer

Accessed without initialization


Error: undefined Reference error Cannot be declared

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

Only one keyword Var Let and const keyword

Function and return keywords used Arrow function new feature dont need function
keyword

Lower performance Higher performance


5. For of and For in
For of For in

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

start index and endindex start index ,length,new elements


9. Map
Map iterates each element of the array and create new array along with conditions.
Syntax
array.map((item,index) => item)

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)

12. Diff b/w Map and ForEach


Map ForEach

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

Used with other array methods like Cannot be used


reduce filter

13. Diff b/w Find and filter


Find Filter

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 }];

let searchitem = "HTML".toLowerCase();


let num = 2014
let out = books.filter((item,index) => item.title.toLowerCase().includes(searchitem)
|| item.year === num)
console.log(out) o/p (2)[..]
3. forEach
Syntax: forEach((item,index,arr) => )
let num=[1,10,1,10,22,4,6,9]
1. Sum of the array o/p:63
let out = 0; num.forEach((numbers) => {out += numbers}) console.log(out);
2. Multiply each and every item of the array and store it in array 0/p: [2,20,2]
const dbmnum =[]
num.forEach((nums,index,arr) =>{dbmnum.push(nums*2)})
console.log(dbmnum);
3. Max element of the array 0/p: 22
let max = num[0]
num.forEach((item,index) => {if(item > max){max = item}})console.log(max)
4. Average of the array o/p:7.8
let out = 0; num.forEach((numbers) => {out += numbers}) console.log(out);
5. let fruits = ['apple', 'banana', 'orange']; //o/p: apple,banana,orange
let out = fruits.forEach((item,index) => item)
6. Change to uppercase in the same array o/p: ['APPLE', 'BANANA', 'ORANGE']
let fruits = ['apple', 'banana', 'orange'];
fruits.forEach((item,index) =>{fruits[index] = item.toUpperCase();})console.log(fruits)
4. Reduce Example
Syntax: reduce((acc,curr,index,arr) => {return acc;}, initialvalue)
let arr = [1,2,3,4,5]
1. sum array
let sum = arr.reduce((acc,curr) => acc + curr, 0); console.log(sum); o/p: 157
2. Flattening array
let nestarr= [1,2,3,4,5,[11,12],3,[4,5]]
let sum = nestarr.reduce((acc,curr) => acc.concat(curr), [])
console.log(sum); o/p: (10) [....]
3. Counting the frequency
let str = ["apple","mango","orange","apple","pineapple","mango"]
● let out = str.reduce((acc,curr) => {acc[curr] = (acc[curr] || 0) + 1;
return acc; },{}) console.log(out)
● let out = str.reduce((acc,curr) => {if(curr in acc){acc[curr]++}else{acc[curr] = 1}
return acc;}, {}). o/p: apple: 2, mango:2,1,1,1
console.log(out);
4. Filter duplicate value
let num = [1,10,1,10,22,4,6,9];
let out = num.reduce((acc,curr) => {
if(!acc.includes(curr)){acc.push(curr)}return acc;}, []); console.log(out)
5. Grouping objects by property
let data = [{ category: "Fruit", name: "Apple" },{ category: "Fruit", name: "Banana" },{
category: "Vegetable", name: "Carrot" }, { category: "Fruit", name: "Orange" },];
● let out = data.reduce((acc,curr) => {
acc[curr.category] = acc[curr.category] || []
acc[curr.category].push(curr.name);return acc;}, {})
● let out = data.reduce((acc,curr) => {
if(curr.category in acc){acc[curr.category].push(curr)}
else{acc[curr.category] = [curr]} return acc;}, {});
o/p: {fruit: 3,veg: 1}
5. Includes Example
Syntax: includes(value,starindex)
let sta = ["apple","bananan","orange","apple"]
console.log(sta.includes("apple")); // o/p: true
console.log(sta.includes("apples")); o/p: false
console.log(sta.includes("apple",2)); o/p: true
6. Slice Example
Syntax: slice(starindex,end)
let num = [1,10,1,10,22,4,6,9];
1. let out = num.slice(); o/p: (8) [1, 10, 1, 10, 22, 4, 6, 9]
2. let out1 = num.slice(0,2); o/p: (2) [1, 10]
3. let out2 = num.slice(2,4); o/p: (2) [1, 10]
4. let out3 = num.slice(4); o/p: (4) [22, 4, 6, 9]
5. let out4 = num.slice(4,2); o/p: []
7. Splice Example
Syntax: splice(start,length,new element)
let num = [1,10,1,10,22,4,6,9];
1. let out = num.splice(2) o/p: [1, 10, 22, 4, 6, 9] num: [1,10]
2. let out = num.splice(3,2) o/p: [10, 22] num: [1, 10, 1, 4, 6, 9]
3. let out1 = num.splice(3,2,50,50) o/p: (2) [10, 22] num:(8) [1, 10, 1, 50, 50, 4, 6, 9]
4. let out1 = num.splice(3,0,50,50) num: (10) [1, 10, 1, 50, 50, 10, 22, 4, 6, 9]

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);

You might also like