Javascript Interview Concepts
Javascript Interview Concepts
JavaScript
JavaScript
Concepts for Interviews
*Disclaimer*
Everyone learns uniquely.
include:
value.
following ways:
Important note:
typeof null returns "object" even though it's a primitive value.
EASY
Using the Object() constructor:
9 This constructor creates a new object wrapper for a value.6
9 If a value is primitive, it will be equal to its object-wrapped
version.6
9 If a value is non-primitive, it won't be equal to its object-wrapped
version.
Javascript
// Using the Object() constructor:
Scope Functional
Block scope Block scope
scope
Can be
Can be updated Cannot be
Update/
updated and
but cannot be re- updated or re-
Re-declaration re-declared declared within declared within
within the scope the scope the scope
Declaration
Can be declared Can be declared Cannot be
without without being without being declared without
Initialization initialized initialized being initialized
return a + b;
};
In this example, the arrow function add takes two parameters (a and
b) and returns their sum. The => syntax is used to define the function,
and the body of the function is enclosed in curly braces {}. If there's
only one expression in the function body, you can omit the curly
braces and the return keyword:
Javascript
const add = (a, b) => {
return a + b;
};
MEDIUM
Here is an example showing how both traditional function expression
and arrow function to illustrate the difference in handle the this
keyword.
// Define an object
let obj1 = {
value: 42,
valueOfThis: function() {
};
console.log(obj1.valueOfThis()); // Output: 42
Arrow Function:
Javascript
let obj2 = {
value: 84,
valueOfThis: () => {
this case)
};
In the arrow function within obj2, this does not refer to obj2. Instead,
it inherits its value from the parent scope, which is the global object
While myMessage appears declared after its use, it's hoisted to the
top of the scope, allowing its reference (but not its initial value) before
the actual declaration line.
Example 2: Function Hoisting
Javascript
sayHello(); // Outputs "Hello, world!"
function sayHello() {
console.log("Hello, world!");
}
MEDIUM
Javascript
function performTask() {
var result;
performTask();
Hoisting also occurs within local scopes, like functions. Here, result
Key Points:
function strict_function() {
'use strict';
x = 'Test message';
console.log(x);
Javascript
function createMultiplier(factor) {
Here are some common scenarios that affect the value of this:
Global Context:
When this is used outside of any function or method, it refers to the
global object (in a browser environment, it usually refers to window).
Javascript
console.log(this); // refers to the global object
(e.g., window in a browser)
Method Invocation:
When a function is a method of an object, this refers to that object.
MEDIUM
Javascript
const myObject = {
myMethod: function() {
};
myObject.myMethod();
Constructor Function:
When a function is used as a constructor with the new keyword, this
refers to the newly created instance of the object.
Javascript
function MyClass() {
There are two main types of scope in JavaScript: global scope and
local scope.
Global Scope:
[ Variables declared outside of any function or block have global
scope.E
[ Global variables are accessible throughout the entire code,
including within functions.
Javascript
var globalVar = "I am global";
function exampleFunction() {
exampleFunction();
function exampleFunction() {
exampleFunction();
Scope Chain:
The scope chain refers to the hierarchy of scopes in a program. When
a variable or function is referenced, JavaScript looks for it in the
current scope and then traverses up the scope chain until it finds the
variable or reaches the global scope.
MEDIUM
Javascript
function mainFunction(){
innerFunction1();
innerFunction2();
mainFunction();
MEDIUM
function outerFunction() {
function innerFunction() {
let innerVariable = 5;
return innerFunction;
closureFunction();
MEDIUM
outerFunction defines an outer variable (outerVariable) and an
inner function (innerFunction).
function sayHello(greeting) {
function sayHello(greeting) {
These methods are especially useful when dealing with functions that
are part of objects or classes, and you want to explicitly set the
context (this) for their execution.
MEDIUM
Q.17
Explain call(), apply() and bind()
methods in Javascript.
In JavaScript, the call, apply, and bind methods are used to
manipulate how a function is invoked and set the value of this within
the function.
call method:
The call method is used to invoke a function with a specified this
value and arguments provided individually.
Javascript
function sayHello(greeting) {
Javascript
let total = 0;
function addToTotal(value) {
total += value;
addToTotal(5);
console.log(total); // Output: 5
this.name = name;
Person.prototype.greet = function() {
};
MEDIUM
Javascript
Javascript
function customGreeting(name) {
function outerFunction(callback) {
callback(name);
outerFunction(customGreeting);
Javascript
getUser(function(user) {
getProfile(user.id, function(profile) {
getPosts(user.id, function(posts) {
});
});
});
});
MEDIUM
Q.20 What is Temporal Dead Zone in
Javascript?
The Temporal Dead Zone is a phenomenon in JavaScript associated
with the use of the let and const keywords, unlike the var keyword. In
ECMAScript 6, attempting to access a let or const variable before it is
declared within its scope results in a ReferenceError. The term
"temporal dead zone" refers to the timeframe during which this
occurs, spanning from the creation of the variable's binding to its
actual declaration.
var value1 = 1;
let value2 = 2;
Javascript
});
It's important to note that the rest parameter must be the last
parameter in the function declaration. For example, this is valid:
MEDIUM
Javascript
// code here
// code here
}
HARD
Q.23 What are generator functions in
Javascript?
In JavaScript, generator functions are a special kind of function that
allows you to control the execution flow and pause/resume it at
certain points. Generator functions are defined using the function*
syntax and use the yield keyword to produce a sequence of values.
When a generator function is called, it returns an iterator called a
generator.
yield 1;
yield 2;
yield 3;
} // Creating a generator
In this example:
O The function* simpleGenerator() syntax defines a generator
function.@
O The yield keyword is used to produce values. Each time yield is
encountered, the generator pauses its execution, and the yielded
value is returned to the caller along with done: false. The
generator can be resumed later.@
O The generator.next() method is used to advance the
generator's execution. It returns an object with two properties:
value (the yielded value) and done (a boolean indicating whether
the generator has finished).
return a + b;
Example:
Javascript
return a + b;
};
hoisted in the same way. If you try to call a function expression before
function.
HARD
What is the difference between setTimeout,
Q.25 setImmediate and process.nextTick?
}, 1000);
C setImmediate:
` Schedules the callback to be executed in the next iteration of the
event loop.Z
` It's often used when you want the callback to be executed
immediately after the current event loop cycle.
HARD
Javascript
setImmediate(() => {
});
:3 process.nextTick:
O Executes the callback after the current event loop cycle, but
before the event loop continues processing other I/O events.L
O It is often used when you want to execute a callback after the
current operation but before I/O events.
Javascript
process.nextTick(() => {
});
Why
Bosscoder?
750+ Alumni placed at Top
Product-based companies.
Explore More