在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:learning-zone/javascript-interview-questions开源软件地址:https://github.com/learning-zone/javascript-interview-questions开源编程语言:HTML 89.4%开源软件介绍:JavaScript Interview Questions ( vES6 )Click if you like the project. Pull Request are highly appreciated. Related Interview QuestionsTable of Contents
# 1. IntroductionQ 1.1. List out important features of JavaScript ES6?1. Template Strings: Template literals are string literals allowing embedded expressions. Benefits:
// String Substitution
let name = `Abhinav Sharma`;
console.log(`Hi, ${name}!`); // Output: "Abhinav Sharma"
// Multiline String
let msg = `Hello \
World`;
console.log(`${msg}`); // Output: "Hello World" ⚝ Try this example on CodeSandbox 2. Spread Operator: Spread operator allows iterables( arrays / objects / strings ) to be expanded into single arguments/elements. function sum(x, y, z) {
return x + y + z;
}
const numbers = [10, 20, 30];
// Using Spread Operator
console.log(sum(...numbers)); // 60
// Using Apply (ES5)
console.log(sum.apply(null, numbers)); // 60 ⚝ Try this example on CodeSandbox 2.1. Copying an array: let fruits = ["Apple", "Orange", "Banana"];
let newFruitArray = [...fruits];
console.log(newFruitArray);
//Output
['Apple','Orange','Banana'] ⚝ Try this example on CodeSandbox 2.2. Concatenating arrays: let arr1 = ["A", "B", "C"];
let arr2 = ["X", "Y", "Z"];
let result = [...arr1, ...arr2];
console.log(result);
// Output
['A', 'B', 'C', 'X', 'Y', 'Z'] ⚝ Try this example on CodeSandbox 2.3. Spreading elements together with an individual element: let fruits = ["Apple", "Orange", "Banana"];
let newFruits = ["Cherry", ...fruits];
console.log(newFruits);
// Output
['Cherry', 'Apple','Orange','Banana'] ⚝ Try this example on CodeSandbox 2.4. Spreading elements on function calls: let fruits = ["Apple", "Orange", "Banana"];
const getFruits = (f1, f2, f3) => {
console.log(`Fruits: ${f1}, ${f2} and ${f3}`);
};
getFruits(...fruits);
// Output
Fruits: Apple, Orange and Banana ⚝ Try this example on CodeSandbox 2.5. Spread syntax for object literals: var obj1 = { id: 101, name: 'Rajiv Sandal' }
var obj2 = { age: 35, country: 'INDIA'}
const employee = { ...obj1, ...obj2 }
console.log(employee); // { "id": 101, "name": "Rajiv Sandal", "age": 35, "country": "INDIA" } 3. Sets: Sets are a new object type with ES6 (ES2015) that allow to create collections of unique values. The values in a set can be either simple primitives like strings or integers, but more complex object types like object literals or arrays can also be part of a set. let numbers = new Set([10, 20, 20, 30, 40, 50]);
console.log(numbers); // Set(5) {10, 20, 30, 40, 50}
console.log(typeof numbers); // Object ⚝ Try this example on CodeSandbox 4. Default Parametrs: function add(x = 10, y = 20) {
console.log(x + y);
}
add(10, 30); // 40 ⚝ Try this example on CodeSandbox 5. repeat(): The const msg = "Hello World \n";
console.log(`${msg.repeat(3)}`);
// Output:
Hello World
Hello World
Hello World ⚝ Try this example on CodeSandbox 6. Arrow Function (=>): let add = (x, y) => x + y;
console.log(add(10, 20)); // 30 ⚝ Try this example on CodeSandbox 7. Arrow function with /**
* Using ES5
*
**/
var person = {
name: "Diksha",
actions: ["bike", "hike", "ski", "surf"],
printActions: function() {
var _this = this;
this.actions.forEach(function(action) {
var str = _this.name + " likes to " + action;
console.log(str);
});
}
};
person.printActions();
/**
* Using Arrow function
*
**/
let person = {
name: "Diksha",
actions: ["bike", "hike", "ski", "surf"],
printActions() {
this.actions.forEach((action) => {
let str = this.name + " likes to " + action;
console.log(str);
});
}
};
person.printActions();
// Output:
Diksha likes to bike
Diksha likes to hike
Diksha likes to ski
Diksha likes to surf ⚝ Try this example on CodeSandbox 8. Destructing Assignment: const phone = {
title: "iPhone",
price: 999,
description: "The iPhone is a smartphone developed by Apple"
};
console.log(phone.title);
// Destructing Assignment
const { title, price, description } = {
title: "iPhone",
price: 999,
description: "The iPhone is a smartphone developed by Apple"
};
console.log(title); // iPhone
console.log(price); // 999
console.log(description); // The iPhone is a smartphone developed by Apple ⚝ Try this example on CodeSandbox 9. Generators: A generator is a function that can stop midway and then continue from where it stopped. In short, a generator appears to be a function but it behaves like an function* generator(num) {
yield num + 10;
yield num + 20;
yield num + 30;
}
let gen = generator(10);
console.log(gen.next().value); // 20
console.log(gen.next().value); // 30
console.log(gen.next().value); // 40 ⚝ Try this example on CodeSandbox 10. Symbols: They are tokens that serve as unique IDs. We create symbols via the factory function Symbol(). Symbols primary use case is for making private object properties, which can be only of type String or Symbol (Numbers are automatically converted to Strings). const symbol1 = Symbol();
const symbol2 = Symbol(42);
const symbol3 = Symbol("Hi");
console.log(typeof symbol1); // symbol
console.log(symbol3.toString()); // Symbol(Hi)
console.log(Symbol("Hi") === Symbol("Hi")); // false ⚝ Try this example on CodeSandbox 11. Iterator: The iterable is a interface that specifies that an object can be accessible if it implements a method who is key is const title = "ES6";
const iterateIt = title[Symbol.iterator]();
console.log(iterateIt.next().value); //output: E
console.log(iterateIt.next().value); //output: S
console.log(iterateIt.next().value); //output: 6 ⚝ Try this example on CodeSandbox # 2. VARIABLESQ 2.1. What are global variables?Global variables are declared outside of a function or declared with a window object for accessibility throughout the program (unless shadowed by locals). If you declare a variable without using var, even if it's inside a function, it will still be seen as global. The Example: var x = 10;
if (x === 10) {
var x = 20;
console.log(x);
// expected output: 20
}
console.log(x);
// expected output: 20 Example: Declaring global variable within function window.value = 90;
// Declaring global variable by window object
function setValue() {
window.value = 100;
}
// Accessing global variable from other function
function getValue() {
setValue();
return window.value;
}
console.log(getValue()); // 100 Using Undeclared Variables:
⚝ Try this example on CodeSandbox Q 2.2. What are template literals in es6?Template literals help make it simple to do string interpolation, or to include variables in a string. const person = { name: 'Tyler', age: 28 };
console.log(`Hi, my name is ${person.name} and I am ${person.age} years old!`);
// 'Hi, my name is Tyler and I am 28 years old!' Template literals, however, preserve whatever spacing you add to them. For example, to create that same multi-line output that we created above, you can simply do: console.log(`This is line one.
This is line two.`);
// This is line one.
// This is line two. Another use case of template literals would be to use as a substitute for templating libraries for simple variable interpolations: const person = { name: 'Tyler', age: 28 };
document.body.innerHTML = `
<div>
<p>Name: ${person.name}</p>
<p>Name: ${person.age}</p>
</div>
`
Q 2.3. What are the differences between variables created using |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论