JavaScript Interview Questions for Experienced
- Arrow Functions: Arrow functions are a more concise syntax for writing function expressions in JavaScript. They don’t have their own
this
,arguments
,super
, ornew.target
, which makes them useful for non-method functions and callbacks. Example:const add = (a, b) => a + b;
- Prototype Design Pattern: This pattern involves creating new objects by copying an existing object, known as the prototype. Itâs useful for creating objects that share a common prototype and thus can inherit properties and methods from it. Itâs commonly used to avoid duplicating code.
var
,let
, andconst
:var
: Function-scoped or globally-scoped; allows re-declaration and updating.let
: Block-scoped; allows updating but not re-declaration in the same scope.const
: Block-scoped; used for variables that wonât be reassigned.
- Rest Parameter and Spread Operator:
- Rest Parameter (
...
): Used to collect multiple arguments into an array in function definitions. Example:function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
- Spread Operator (
...
): Expands an array or object into individual elements. Example:const arr = [1, 2, 3];
const newArr = [...arr, 4, 5]; // [1, 2, 3, 4, 5]
- Rest Parameter (
- Creating Objects:
- Object Literals:
const obj = { key: 'value' };
- Constructor Functions:
function Person(name) {
this.name = name;
}
- ES6 Classes:
class Person {
constructor(name) {
this.name = name;
}
}
- Object.create():
const obj = Object.create(proto);
- Factory Functions:
function createPerson(name) {
return { name };
}
- Object Literals:
- Promises: Promises are objects representing the eventual completion or failure of an asynchronous operation. They help manage asynchronous code and avoid “callback hell”. Example:
const fetchData = () => new Promise((resolve, reject) => {
setTimeout(() => resolve('Data received'), 1000);
});
- Classes: Classes are a syntactic sugar over JavaScriptâs prototype-based inheritance. They simplify the creation of objects and inheritance. Example:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
- Generator Functions: Generator functions use
function*
syntax and can be paused and resumed. They return an iterator, which can be used to generate a sequence of values. Example:function* generator() {
yield 1;
yield 2;
yield 3;
}
- WeakSet: A
WeakSet
is a collection of objects where the objects are weakly referenced, meaning they can be garbage collected if there are no other references. It does not prevent the garbage collection of its elements. - Callbacks: Callbacks are functions passed as arguments to other functions to be executed later. They help handle asynchronous operations and ensure code execution order. Example:
function fetchData(callback) {
setTimeout(() => callback('Data received'), 1000);
}
- WeakMap: A
WeakMap
is a collection of key-value pairs where the keys are objects and the values are arbitrary values. It doesnât prevent the garbage collection of its keys. - Object Destructuring: Destructuring allows unpacking values from arrays or properties from objects into distinct variables. Example:
const { name, age } = person;
- Prototypal vs Classical Inheritance:
- Prototypal: Objects inherit directly from other objects. In JavaScript, this is done via prototypes.
- Classical: Classes (blueprints for objects) are used, and objects are instances of classes. This is common in languages like Java and C++.
- Temporal Dead Zone (TDZ): The period from the start of a block until the variable is declared and initialized when using
let
andconst
. Accessing a variable in this period results in aReferenceError
. - JavaScript Design Patterns: Patterns are standard solutions to common problems in software design. Examples include Singleton, Module, and Observer patterns.
- Pass-by-Reference vs Pass-by-Value: JavaScript is pass-by-value for primitive types and pass-by-reference for objects. This means primitives are copied and objects are referenced.
- Async/Await vs Generators:
- Async/Await: Provides a more readable and synchronous-looking code for asynchronous operations.
- Generators: Provide a way to pause and resume functions, which can also be used for asynchronous programming but often with more boilerplate.
- Primitive Data Types:
undefined
,null
,boolean
,number
,bigint
,string
, andsymbol
. - Deferred Scripts: Scripts with the
defer
attribute are executed in the order they appear in the document, after the HTML is parsed. This helps in optimizing page load times. - Lexical Scoping: To use lexical scoping, you need to understand that the scope of a variable is determined by its position in the source code. Nested functions can access variables from their outer functions.
- Example JavaScript Code Purpose: To provide a specific example, you would need to share the code in question.
0 Comments