0% found this document useful (0 votes)
39 views

Javascript Interview Question PDF by Relaxbyte Watermark

The document explains different JavaScript concepts like data types, hoisting, the debugger keyword, equality operators, block scoping with let and var, implicit type coercion, static vs dynamic typing, NaN, pass by value vs reference, IIFEs, strict mode, higher order functions, the this keyword, self-invoking functions, call/apply/bind methods, currying, external JavaScript, scope and scope chain, and closures.

Uploaded by

Noman Naeem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Javascript Interview Question PDF by Relaxbyte Watermark

The document explains different JavaScript concepts like data types, hoisting, the debugger keyword, equality operators, block scoping with let and var, implicit type coercion, static vs dynamic typing, NaN, pass by value vs reference, IIFEs, strict mode, higher order functions, the this keyword, self-invoking functions, call/apply/bind methods, currying, external JavaScript, scope and scope chain, and closures.

Uploaded by

Noman Naeem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What are the different data types present in JavaScript?

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.

2. Explain Hoisting in JavaScript.


Code Example:

console.log(x); // Output: undefined


var x = 5;
// This is because variable declarations are hoisted to the top of their scope.

Explanation: Hoisting is a JavaScript behavior where variable and function declarations are moved to
the top of their scope during compilation.

3. Why do we use the word “debugger” in JavaScript?


Code Example:

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:

console.log(5 == '5'); // Output: true


console.log(5 === '5'); // Output: false

Explanation: The "==" operator checks for equality of values, while the "===" operator checks for
both equality of values and types.

5. Difference between var and let keyword in JavaScript.


Code Example:

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.

6. Explain Implicit Type Coercion in JavaScript.


Code Example:

console.log('5' + 5); // Output: '55'


console.log('5' - 1); // Output: 4 (coerces '5' to a number)

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.

7. Is JavaScript a statically typed or a dynamically typed language?


Explanation: JavaScript is a dynamically typed language where variable types are determined at
runtime.
8. What is NaN property in JavaScript?
Code Example:

console.log(typeof NaN); // Output: 'number'


console.log(NaN === NaN); // Output: false (NaN is not equal to itself)

Explanation: NaN (Not-a-Number) is a special value in JavaScript representing an invalid numerical


operation, typically resulting from mathematical operations involving undefined values or non-
numeric strings.

9. Explain passed by value and passed by reference.


Code Example:

// 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.

10. What is an Immediately Invoked Function in JavaScript?


Code Example:

(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.

11. What do you mean by strict mode in JavaScript and characteristics of


JavaScript strict-mode?
Explanation: Strict mode is a feature introduced in ECMAScript 5 that allows you to place a program
or a function in a strict operating context. It catches common coding mistakes and "unsafe" actions,
making debugging easier and code more secure. Characteristics include disallowing the use of
undeclared variables, throwing errors on assignments to non-writable properties, and disallowing
duplicate property names or parameter values.

12. Explain Higher Order Functions in JavaScript.

const person = {
name: 'John',
greet: function() {
console.log(`Hello, my name is ${this.name}.`);
}
};

person.greet(); // Output: Hello, my name is John.

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.

13. Explain “this” keyword.

const person = {
name: 'John',
greet: function() {
console.log(`Hello, my name is ${this.name}.`);
}
};

person.greet(); // Output: Hello, my name is John.


Explanation: In JavaScript, the this keyword refers to the object it belongs to. Its value is
determined by how a function is called (runtime binding). In the given example, this refers to the
person object when greet method is invoked.

14. What do you mean by Self-Invoking Functions?


Explanation: Self-invoking functions (also known as Immediately Invoked Function Expressions or
IIFEs) are JavaScript functions that are executed as soon as they are defined. They don't need to be
called elsewhere in the code.

15. Explain call(), apply(), and bind() methods.


Explanation: call() , apply() , and bind() are methods used to set the value of this explicitly.
They allow borrowing methods from other objects, passing arguments, and creating a new function
with a fixed this value, respectively.

16. What is currying in JavaScript?

function add(a) {
return function(b) {
return a + b;
};
}

const addTwo = add(2);


console.log(addTwo(3)); // Output: 5

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.

17. What are some advantages of using External JavaScript?


Explanation:

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.

18. Explain Scope and Scope Chain in JavaScript.


Explanation:

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.

19. Explain Closures in JavaScript.


Code Example:

function outerFunction() {
const outerVariable = 'I am from the outer function';
return function innerFunction() {
console.log(outerVariable);
};
}

const innerFunc = outerFunction();


innerFunc(); // Output: I am from the outer function

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.

You might also like