Top JavaScript interview questions
******************************In progress
- JavaScript is a scripting language used to create dynamic and interactive websites. It is supported by all major web browsers.
- JavaScript is basically a client-side scripting language but it can also be used on the server-side with the help of technologies such as Node.js.
Primitive | Non-primitive |
---|---|
Boolean, NULL, undefined, BigInt, String, Number, Symbol | Object, Array |
a) Object Literals: A comma-separated set of name and value pairs that is wrapped inside curly braces.
var person = {
name: 'Surbhi',
age: 25,
occupation: 'Software Engineer'
}
b) Object.create method: TCreates a new object, by using an existing object as the prototype of the newly created object.
const person = {
name: 'Surbhi',
age: 25,
occupation: 'Software Engineer'
}
var info = Object.create(person);
console.log(info.name); // output - Surbhi
Here "person" is an existing object which is passed as a prototype to the newly created object "info"
const person = new Person();
class Person {
constructor(name) {
this.name = name;
}
}
let person = new Person('Surbhi');
console.log(person.name); //output - Surbhi
- == : While comparing two operands, checks for only value
- === : While comparing two operands, checks for value as well as data type
- JavaScript is Single-threaded
Arrow functions were introduced in ES6 (ECMAScript 2015) and provide a simpler and compact way to write functions. They are also called “fat arrow functions”
const functionName = (something) => {
return something;
}
- They use a concise syntax which makes them shorter and easier to read as compared to traditional function expressions
- They do not bind their own this value, but instead inherit the this value from the enclosing lexical scope (i.e., the scope in which it is defined)
- They do not have "prototype" property and hence cannot be used as a constructor
- If the arrow function body consists of a single expression, that expression will be implicitly returned, without the need for a return statement.
- If an arrow function has only one parameter, the parentheses around the parameter list can be omitted.
Synthetic events are objects that act as a cross-browser wrapper around a browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
Array destructuring is a unique feature in JavaScript that allows you to extract array's value into new variables. For e.g.,
const arr = [1,2,3];
const [num1, num2, num3] = arr;
console.log(num1); // output =====> 1
console.log(num2); // output =====> 2
console.log(num3); // output =====> 3
- Yes, using Spread Operators
const arr1 = [1,2,3,4];
const arr2 = [5,6];
const arr3 = [...arr1, ...arr2]
console.log(arr3) // output =====> [1,2,3,4,5,6]
JavaScript is a dynamically typed language, because the type of a variable is determined at runtime based on the value it holds. For e.g.,
var variable_one = "surbhi" // "string" datatype is determined at runtime
var variable_two = 20 // "number" datatype is determined at runtime
Undeclared - A variable that has not been declared or doesn't exists in the code
console.log(a); // output =====> ReferenceError: a is not defined
Undefined - A variable that has been declared but not assigned a value yet
let a;
console.log(a); // output =====> undefined
Null - Null means an empty value or a blank value. It is used to represent an intentional absence of value.
let demo = null;
console.log(demo); // output =====> null
Undefined - Undefined means the variable has been declared, but its value has not been assigned yet.
let demo;
console.log(demo); // output =====> undefined
A "let" or "const" variable is said to be in a "temporal dead zone" (TDZ) from the start of the block until code execution reaches the line where the variable is declared and initialized.
In JavaScript, variables declared with let or const are not hoisted to the top of the block scope. If you try to access a variable before it is declared, a ReferenceError will be thrown.
{
// TDZ for name variable starts here
console.log(name); // ReferenceError
let name = "surbhi"; // End of TDZ for name
}
IIFE stands for Immediately Invoked Function Expression. It is a JavaScript function that is executed as soon as it is defined.
An IIFE is typically written as an anonymous function expression that is enclosed within a set of parentheses, followed by another set of parentheses that call the function. For e.g.,
(function() {
// code goes here
})();
Hoisting is a default behavior in JavaScript where variable and function declarations are moved to the top of the current scope
Variable hoisting
console.log(a); // output =====> undefined
var a = 10;
The output of above code will be undefined because the declaration of "a" is hoisted to the top of the scope, but the initialization happens later in the code.
a=10;
console.log(a); // output =====> 10
var a;
The output of above code will be 10 because the variable "a" is hoisted to the top of the scope and its value is assigned before console.log
Function hoisting
demo(); // demo console
function demo() {
console.log('demo console'); //
}
The output of above code will be "demo console" because the function declaration is hoisted to the top of the scope, allowing it to be called before it is declared in the code.
**Note**
+ Only function declarations are hoisted, not the function expressions.
+ Only the declaration is hoisted, not the initialization.
******************************In progress