InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is the result of the spread operator array shown below? |
|
Answer» [...'apple'] Output: ['a', 'p', 'p', 'l', 'e'] Explanation: A STRING is an iterable type, and in an ARRAY, the spread operator transfers each character of an iterable to one element. As a result, each character in a string becomes an Array element. |
|
| 2. |
How do you use Destructuring Assignment to swap variables? |
| Answer» VAR a = 1, B = 2;[a, b] = [b, a];console.log(a); // 2console.log(b); // 1 | |
| 3. |
Compare the ES5 and ES6 codes for object initialization and parsing returned objects. |
|
Answer» Object initialization: VARIABLES with the same name are frequently used to create object PROPERTIES. Consider the following scenario: // ES5 codevar x = 1, y = 2, z = 3; ob = { x : a, y : b, z : z };// ob.x = 1, ob.y = 2, ob.z = 3In ES6, there's no need for tedious repetition! // ES6 codeconst x = 1, y = 2, z = 3; ob = { x y z };// ob.x = 1, ob.y = 2, ob.z = 3Parsing returned objects: Only one value can be returned by a function, but that value could be an object with hundreds of properties and/or methods. In ES5, you must first get the returned object and then extract values from it. Consider the following scenario: // ES5 codevar ob = getObject(), a = ob.a, b = ob.b, c = ob.c;This is MADE easier by ES6 destructuring, which ELIMINATES the need to keep the object as a variable: // ES6 codeconst { a , b , c } = getObject(); |
|
| 4. |
Name some string functions introduced in ES6. |
||||||||||
Answer»
|
|||||||||||
| 5. |
Name some array methods that were introduced in ES6. |
||||||||||||||||||||
Answer»
|
|||||||||||||||||||||
| 6. |
What is Babel? |
|
Answer» Babel is an open-source JAVASCRIPT transpiler that converts ECMAScript 2015+ (ES6+) code into a backwards compatible version of JavaScript that can be run by previous JavaScript engines. Babel is a popular tool for exploiting the JavaScript programming language's latest CAPABILITIES. Babel plugins are used to convert SYNTAX that isn't widely supported into a version that is backwards compatible. Arrow functions, for example, which are defined in ES6, are translated to ORDINARY function declarations. It's also possible to translate non-standard JavaScript syntax, such as JSX. Babel may automatically inject core-js polyfills for support capabilities that aren't available in JavaScript environments. Static methods like Array.from and built-ins like Promise, for example, are only accessible in ES6+, but they can be utilised in previous contexts by using core-js. |
|
| 7. |
What is the reason behind adding Symbol to ES6? |
|
Answer» Symbols are a new type of object that can be USED as distinct property names in objects. Using Symbols instead of strings allows separate modules to create properties that are not mutually exclusive. Symbols can also be KEPT private, preventing anyone who does not have direct access to the SYMBOL from accessing its properties. Symbols are a brand-new kind of primitive. Symbols, like numbers, strings, and booleans, have a function that can be used to produce them. Symbols, unlike the other primitives, do not have a literal SYNTAX (similar to how strings have ") and can only be created using the Symbol constructor: let symbol = Symbol();In truth, Symbols are only a little different means of attaching properties to an object; the well-known Symbols could easily be provided as standard methods, just like Object.prototype.has Own Property which APPEARS in anything that inherits from Object. |
|
| 8. |
What is the difference between for..of and for..in? |
Answer»
Both the for..of and for..in commands ITERATE over lists, but the RESULTS they return are different: for..in returns a list of keys on the object being iterated, whereas for..of returns a list of values of the object's numeric attributes. let arr = [3, 4, 5];for (let i in arr) { console.log(i); // "0", "1", "2",}for (let i of arr) { console.log(i); // "3", "4", "5"} |
|
| 9. |
What is Bubbling and Capturing? |
|
Answer» When an EVENT OCCURS on the DOM, it does not TAKE place completely on one element. The event bubbles up or goes to its parent, grandparents, and grandparent's parent until it reaches the window in the Bubbling PHASE, whereas the event starts out from window down to the element that prompted the event or the event target in the Capturing Phase. There are three stages of event propagation:
|
|
| 10. |
Which keyword can be used to deploy inheritance in ES6? |
|
Answer» The extend keyword is used to implement inheritance in the ES6 language. There was no idea of classes in prior VERSIONS of Javascript, but with the release of ES6, Pure OBJECT ORIENTED ELEMENTS were added to the language. class Classroom { constructor(students) { this.students = students; } room() { console.log('This class has ' + this.students + ' students'); }} class sectionA extends Classroom { constructor(students) { super(students); } sec() { console.log('section A'); }} LET secA = new sectionA(40); secA.room();secA.sec(); |
|
| 11. |
What is Export Default and Named Export in ES6? |
|
Answer» With the help of the import STATEMENT, the export statement comes into picture when one needs to export functions, objects, and variables to other JavaScript modules. There are two methods for exporting:
Example: //file rectangle.jsfunction perimeter(x, y) { return 2 * (x + y);}function area(x, y) { return x * y;}export { perimeter, area }; //while importing the functions in test.jsimport { perimeter, area } from './rectangle;console.log(perimeter(4, 6)) //20console.log(area(4, 6)) //24Output: 2024
Example: // file module.jsvar a = 6; export default a; // test.js// while importing a in test.jsimport b from './module'; console.log(b); // output will be 6Output: 6
Output: This is a default export.3 |
|
| 12. |
What are the states of Promises in ES6? |
|
Answer» Promises mainly possess three states as follows:
The promise will be immutable once it has been fulfilled or rejected. A rejected FUNCTION and a resolve function are the two arguments passed into the Promise() constructor. It returns either the first or second parameter, DEPENDING on the asynchronous OPERATION. |
|
| 13. |
What do you understand about IIFE (Immediately Invoked Function Expressions)? |
|
Answer» IIFE is a JavaScript FUNCTION that starts running as soon as it is defined. The Self-Executing Anonymous Function is ANOTHER name for it. It is divided into two major sections, which are as follows:
Output: Good Day |
|
| 14. |
What do you understand about default parameters? |
|
Answer» If no value or undefined is passed, we can use the default PARAMETERS to set default VALUES for NAMED parameters. VAR display = (x , y = 2) => { console.log(x + " " + y); } display(1);Output: 1 2 |
|
| 15. |
What is a class expression? |
|
Answer» In ES6, one way to define a class is to use the Class expression. Class expressions, like function expressions, can be named or unnamed. If the class is named, the name is unique to the class body. Prototype-based inheritance is used in JavaScript classes. var PRODUCT = class { constructor (num1, num2) { this.num1 = num1; this.num2 = num2; } multiplication() { return this.num1 * this.num2; }}console.log(new Product(5,8).multiplication());// EXPECTED output: 40The syntax of a class expression is similar to that of a class statement (declaration). Class expressions, on the other HAND, allow you to omit the class name (“binding identifier”), which is not possible with class statements. Additionally, unlike class declarations, class expressions allow you to redefine/re-declare classes WITHOUT causing any TYPE errors. It is not required to use the constructor property. The type of classes created with this keyword will always be "function." |
|
| 16. |
How can you create a class in ES6? |
|
Answer» The KEYWORD CLASS is used to create a class in ES6. We can use class expressions or class declarations to INCLUDE classes in our code. Only functions and constructors are allowed in a class definition. These components are collectively referred to as the class's DATA members. Constructors in classes are responsible for ALLOCATING memory to the class's objects. A class's functions are in charge of performing actions on the objects. Syntax: In ES5 var varName = new className { }Syntax: In ES6 (Using class keyword) class className{ } |
|
| 17. |
Why should one use ES6 classes? |
|
Answer» Developers have discovered that ES6 classes are really HANDY. The following are some of the most common applications of ES6 classes:
|
|
| 18. |
Discuss the template literals in ES6. |
|
Answer» Template literals are a brand-new feature in ES6. It makes producing multiline strings and performing string interpolation simple. Template literals, also known as string literals, allow for embedded expressions. Template literals were REFERRED to as template strings prior to ES6. The backtick (``) character is used to enclose template literals. The dollar SIGN and curly BRACKETS (${expression}) are used to denote placeholders in template literals. If we need to use an expression within the BACKTICKS, we can put it in the (${expression}) variable. let s1 = "Good"; let s2 = "Day"; let s = `${s1} ${s2}`; console.log(s);Output: Good Day |
|
| 19. |
Explain the Rest parameter in ES6. |
|
Answer» It's a NEW feature in ES6 that enhances the ability to manage ARGUMENTS. Indefinite arguments can be represented as an array using rest parameters. We can INVOKE a function with any number of parameters by UTILIZING the rest parameter. function DISPLAY(...args) { let ans = 0; for (let i of args) { ans *= i; } console.log("Product = "+ans); } display(4, 2, 3);Output: Product = 24 |
|
| 20. |
What are Promises in ES6? |
|
Answer» Asynchronous programming is a concept found in JavaScript. The processes are run separately from the main thread in asynchronous programming. Promises are the most convenient approach to deal with asynchronous programming in ES6. DEPENDING on the outcome of the procedure, a promise can be refused or RESOLVED. Callbacks were used to COPE with asynchronous programming before promises were introduced in ES6. However, it caused the problem of callback hell, which was ADDRESSED with the introduction of promises. (A callback is a function that is performed after another function has completed. When working with events in JavaScript, callback is very useful. As an ARGUMENT to another function, we pass a function into another function. When we use callbacks in our web applications, it's common for them to get nested. Excessive callback usage clogs up your web application and leads to callback hell.) |
|
| 21. |
Explain Destructuring in ES6. |
|
Answer» Destructuring was INTRODUCED in ES6 as a way to extract DATA from arrays and objects into a SINGLE variable. It is possible to extract smaller fragments from objects and arrays using this method. The following is an example. let greeting =['GOOD','Morning']; let [g1, G2] = greeting; console.log (g1, g2);Output: Good Morning |
|
| 22. |
What is the “spread” operator in ES6? |
|
Answer» The list of parameters is OBTAINED using the spread operator. Three dots (...) are used to represent it. The spread operator divides an iterable (such as an array or a string) into individual elements. It's mostly used in JavaScript to make shallow copies of JS. It improves the READABILITY of your code by making it more concise. The spread operator can be used to join two ARRAYS together or to concatenate them. let arr1 = [4, 5, 6]; let arr2 = [1, 2, 3, ...arr1, 7, 8, 9, 10]; console.log(arr2);Output: [ 1 2 3 4 5 6 7 8 9 10 ] |
|
| 23. |
What is the generator function? |
|
Answer» This is a NEWLY introduced feature in ES6. The Generator function returns an object after GENERATING several values over time. We can iterate over this object and extract values from the function one by one. A generator function returns an iterable object when called. In ES6, we use the * sign for a generator function along with the new ‘yield' keyword. function *Numbers() { let num = 1; while(true) { yield num++; }} var gen = Numbers(); // Loop to print the first// 5 Generated numbersfor (var i = 0; i < 5; i++) { // Generate the next number document.write(gen.next().value); // New Line document.write("<br>");}Output: 12345The yielded value becomes the next value in the sequence each time yield is invoked. Also, generators compute their output values on demand, ALLOWING them to efficiently represent expensive to compute SEQUENCES or even infinite sequences. |
|
| 24. |
When should one not use arrow functions? |
|
Answer» One should not use ARROW FUNCTIONS in the following cases:
As arrow functions are anonymous, we cannot use them when we want function hoisting or when we want to use named functions.
The value of b does not drop when you call a.func. It's because this isn't bound to anything and will inherit the value from its parent scope. var btn = document.getElementById('clickMe');btn.addEventListener('click', () => { this.classList.toggle('on');});We'd get a TypeError if we clicked the button. This is due to the fact that this is not bound to the button, but rather to its parent scope.
Since arrow functions don’t have this/arguments of their own and they depend on their outer context, we cannot use them in cases where we need to use this/arguments in a function. |
|
| 25. |
Discuss the arrow function. |
|
Answer» In ES6, arrow functions are introduced. The shorthand syntax for writing ES6 functions is arrow functions. The arrow function's DEFINITION consists of parameters, followed by an arrow (=>), and the function's body. The 'fat arrow' function is another NAME for the Arrow function. We won't be able to employ them as constructors. CONST function_name = (arg_1, arg_2, arg_3, ...) => { //body of the function }Few things to note:
|
|
| 26. |
What is the difference between let and const? What distinguishes both from var? |
|
Answer» When declaring any variable in JavaScript, we used the var keyword. Var is a function scoped keyword. Within a function, we can access the variable. When we need to CREATE a new scope, we wrap the code in a function. Both LET and const have block scope. If you use these keywords to declare a variable, it will only exist within the innermost block that surrounds them. If you declare a variable with let INSIDE a block (for EXAMPLE, if a condition or a for-loop), it can only be accessed within that block. The variables declared with the let keyword are mutable, which means that their values can be changed. It's akin to the var keyword, but with the added BENEFIT of block scoping. The variables declared with the const keyword are block-scoped and immutable. When variables are declared with the const keyword, their value cannot be modified or reassigned. |
|
| 27. |
Give a thorough comparison between ES5 and ES6. |
||||||||||||
Answer»
|
|||||||||||||
| 28. |
What are the object oriented features supported in ES6. |
|
Answer» The object-oriented features supported in ES6 are:
|
|
| 29. |
Mention some popular features of ES6. |
|
Answer» Some of the common ES6 features are:
|
|