20 Javascript interview questions with code answers.
I am going to start a series for the JavaScript interview questions which may help junior to mid level developers.
Below are 20 common JavaScript ES6+ interview questions with examples.
- What is let and const in JavaScript?
let
and const
are block-scoped declarations in JavaScript, used to declare variables. let
allows you to reassign the value of the variable, while const
creates a read-only reference to a value.
Example:
let message = "Hello, World!";
message = "Hello, JavaScript!";
console.log(message); // Output: "Hello, JavaScript!"
const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable.
2. What is arrow function in JavaScript?
Arrow functions are a shorthand for anonymous functions in JavaScript. They are also known as “fat arrow” functions.
Example:
let add = (a, b) => a + b;
console.log(add(1, 2)); // Output: 3
let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = numbers.map(num => num * 2);
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
3. What is default parameter in JavaScript?
Default parameters allow function parameters to be initialized with default values if…