11. What is the typeof operator?
The typeof operator is used to determine the data type of a value.
Example:
console.log(typeof "Hello"); // "string"
console.log(typeof 100); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
Important Interview Point:
console.log(typeof null);
Output:
"object" This is a historical behavior in JavaScript.
12. What are template literals?
Template literals are strings created using backticks ``.
They allow you to easily embed variables and expressions using
${}.Example:
const name = "Ajay";
const age = 25;
console.log(`My name is ${name} and I am ${age} years old.`);
Output:
My name is Ajay and I am 25 years old.Benefits:
Easy string interpolation
Supports multi-line strings
Allows expressions inside strings
13. What are JavaScript operators?
Operators are symbols used to perform operations on values.
Common Types:
Arithmetic:
+ - * / % **Comparison:
== === != !== > < >= <=Logical:
&& || !Assignment:
= += -= *= /=Example:
const a = 10;
const b = 5;
console.log(a + b); // 15
console.log(a > b); // true
14. What is the ternary operator?
The ternary operator is a short way of writing an
if...else statement.Syntax:
condition ? valueIfTrue : valueIfFalse;
Example:
const age = 20;
const result = age >= 18 ? "Adult" : "Minor";
console.log(result);
Output:
AdultEquivalent if...else:
if (age >= 18) {
result = "Adult";
} else {
result = "Minor";
}Interview Tip: Use ternary operators for simple conditions. Avoid deeply nested ternaries because they reduce readability.
15. What is variable hoisting?
Hoisting is JavaScript's behavior of processing declarations before executing the code in their scope.
With var:
console.log(x);
var x = 10;
Output:
undefined The declaration is hoisted, but the assignment happens later.
With let and const:
console.log(x);
let x = 10;
This results in a
ReferenceError. let and const are hoisted but remain in the Temporal Dead Zone (TDZ) until their declaration is reached.Function declarations are also hoisted:
greet();
function greet() {
console.log("Hello");
}
16. What is scope in JavaScript?
Scope determines where a variable can be accessed in a program.
Example:
function test() {
let message = "Hello";
console.log(message);
}
test();message is accessible inside the function but not outside it.Main Types:
Global Scope
Function Scope
Block Scope
Module Scope
Understanding scope is essential for closures and avoiding variable conflicts.
17. What are global, function, and block scope?
Global Scope
A variable declared outside functions or blocks can generally be accessed throughout the script.
let name = "Deepak";
function greet() {
console.log(name);
}
Function Scope
Variables declared with
var inside a function are accessible throughout that function.