Javascript Interview Question PDF by Relaxbyte Watermark
Javascript Interview Question PDF by Relaxbyte Watermark
Code Example:
// Primitive types
let str = 'Hello'; // String
let num = 10; // Number
let bigNum = 1234567890123456789012345678901234567890n; // BigInt
let bool = true; // Boolean
let und = undefined; // Undefined
let n = null; // Null
let sym = Symbol('foo'); // Symbol
// Non-primitive type
let obj = { name: 'John', age: 30 }; // Object
Explanation: JavaScript supports various data types, including primitive types like strings, numbers,
BigInts, booleans, undefined, null, and symbols, as well as non-primitive types like objects.
Explanation: Hoisting is a JavaScript behavior where variable and function declarations are moved to
the top of their scope during compilation.
function add(x, y) {
debugger;
return x + y;
}
// When executed, the debugger will pause execution at this line, allowing for debugging.
Explanation: The "debugger" keyword is used in JavaScript to activate the debugger, allowing
developers to pause code execution and inspect variables, call stack, etc., for debugging purposes.
4. Difference between “==” and “===” operators.
Code Example:
Explanation: The "==" operator checks for equality of values, while the "===" operator checks for
both equality of values and types.
if (true) {
var a = 10;
let b = 20;
}
console.log(a); // Output: 10
console.log(b); // Error: b is not defined (let has block scope)
Explanation: "var" has function scope, while "let" has block scope, meaning variables declared with
"let" are only accessible within the block they are defined in.
Explanation: Implicit type coercion is the automatic conversion of values from one data type to
another, often occurring in situations like arithmetic operations, string concatenation, and
comparisons.
// Passed by value
let a = 10;
let b = a;
a = 20;
console.log(b); // Output: 10
// Passed by reference
let obj1 = { name: 'John' };
let obj2 = obj1;
obj1.name = 'Doe';
console.log(obj2.name); // Output: Doe
Explanation:
Passed by value: When a primitive data type (like numbers or strings) is assigned to another
variable, a copy of the value is made. Changes made to one variable do not affect the other.
Passed by reference: When non-primitive data types (like objects or arrays) are assigned to
another variable, a reference to the original data is passed. Both variables point to the same
object in memory, so changes made to one variable affect the other.
(function() {
console.log('This is an Immediately Invoked Function Expression (IIFE).');
})();
Explanation: An Immediately Invoked Function Expression (IIFE) is a JavaScript function that runs as
soon as it is defined. It is executed immediately after its definition and does not require a function
name.
const person = {
name: 'John',
greet: function() {
console.log(`Hello, my name is ${this.name}.`);
}
};
Explanation: Higher-order functions are functions that can take other functions as arguments or
return functions as output. They enable functional programming paradigms like callback functions,
map, reduce, filter, etc.
const person = {
name: 'John',
greet: function() {
console.log(`Hello, my name is ${this.name}.`);
}
};
function add(a) {
return function(b) {
return a + b;
};
}
Explanation: Currying is the process of transforming a function that takes multiple arguments into a
sequence of functions that each take a single argument. This allows for partial application of the
function, where you can fix some of the arguments and generate a new function that takes the
remaining arguments.
Code Modularity: External JavaScript files promote code modularity by separating logic into
different files, making it easier to manage and maintain
.
Caching: External JavaScript files can be cached by the browser, reducing load times for
subsequent page visits.
Improved Performance: By separating JavaScript code into external files, the initial HTML file size
is reduced, leading to faster page loading.
Ease of Maintenance: Changes or updates to JavaScript code can be made in a single external
file, simplifying maintenance across multiple web pages.
Scope: Scope refers to the accessibility of variables in JavaScript. It defines where variables and
functions are accessible or visible within your code. JavaScript has function-level scope, meaning
variables defined inside a function are not accessible from outside the function.
Scope Chain: The scope chain is the mechanism that determines the accessibility of variables
within nested functions. When a variable is referenced, JavaScript first looks for it in the current
function's scope. If not found, it traverses up the scope chain, checking each level until it reaches
the global scope.
function outerFunction() {
const outerVariable = 'I am from the outer function';
return function innerFunction() {
console.log(outerVariable);
};
}
Explanation: Closures are an essential concept in JavaScript where an inner function has access to the
variables and parameters of its outer function, even after the outer function has finished executing. In
the example, innerFunction maintains a reference to the outerVariable even after outerFunction
has completed execution.