JavaScript Question’s Must To Know

Md Piash
5 min readMay 17, 2020

1.Different between some() and every()?

The some() method tests whether some element in the array passes the test implemented by the provided function. The every() method tests whether all elements in the array pass the test implemented by the provided function. So you will use them, according if you want to test some elements or every elements.

Example:

const array = [1, 2, 3, 4, 5];
const evenArr = [2, 4, 6];
const even = (element) => {
return element % 2 === 0;
};

console.log(array.some(even)); // true
console.log(array.every(even)) // false
console.log(evenArr.every(even)) // true

2. Closures

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state.In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time. To use a closure, define a function inside another function and expose it.

Example:

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

var add5 = makeAdder(5);
var add20 = makeAdder(20);

console.log(add5(6)); // 15
console.log(add20(7)); // 57

3.Reverse String

Step By Step Example of Reverse String is given bellow:

function reverseString(str) {
// Step 1. Use the split() method to return a new array
var splitString = str.split(“”); // var splitString = “hello”.split(“”);
// [“h”, “e”, “l”, “l”, “o”]
// Step 2. Use the reverse() method to reverse the new created array
var reverseArray = splitString.reverse(); // var reverseArray = [“h”, “e”, “l”, “l”, “o”].reverse();
// [“o”, “l”, “l”, “e”, “h”]
// Step 3. Use the join() method to join all elements of the array into a string
var joinArray = reverseArray.join(“”); // var joinArray = [“o”, “l”, “l”, “e”, “h”].join(“”);
// “olleh”
//Step 4. Return the reversed string
return joinArray; // “olleh”
}

console.log(reverseString(“Abrar”));

4.JavaScript Prototype

JavaScript is often described as a prototype-based language to provide inheritance, objects can have a prototype object, which acts as a template object that it inherits methods and properties from.In javaScript every function has a property called Prototype.By default this property is empty but we can add properties and methods to it.

Example:

let EmployeeDetails = function(name, age){
this.name = name;
this.age = age;
// this.getName = function(){
// return this.name;
// };

// this.getAge = function(){
// return this.age;
// };
};

EmployeeDetails.prototype.getName = function(){
return this.name;
};

EmployeeDetails.prototype.getAge = function(){
return this.age;
};

let emp1 = new EmployeeDetails(‘Abrar’, 23);
let emp2 = new EmployeeDetails(‘Piash’, 24);

console.log(emp1.getName(),emp1.getAge());
console.log(emp2.getName(),emp2.getAge());

Suppose there is a function X when we crate another function that is inherited from X it will inherit the methods and properties defined in X’s prototype.

5.Constructor

A constructor is a function that creates an instance of a class which is typically called an “object”. In JavaScript, a constructor gets called when we declare an object using the new keyword. The purpose of a constructor is to create an object and set values if there are any object properties present. It’s a neat way to create an object because you do not need to explicitly state what to return as the constructor function, by default, returns the object that gets created within it.

Example:

function User(first, last) {
this.firstName = first
this.lastName = last

}

var user1 = new User(“Abrarul”, “Islam”);
console.log(user1);
var user2 = new User(“Md”, “Piash”);
console.log(user2);

6. Hoisting:

ECMAScript6 brings a new feature called hoisting. In general, hoisting is a mechanism which handles execution contexts in JavaScript. This means the variable and function are put into the memory during the compile phases before going for any execution. Var declarations are treated as if they are hoisted to the top of the function or to the global scope if they are declared outside a function. So JavaScript engine treats the previous function internally as the following:

Example:

function getName(me) {
var name
if (me) {
name = ‘Abrar’
console.log(name)// Abrar if true
}

else {
console.log(name) //Abrar if false
}

console.log(name) // Abrar
}
getName(true);

In this example the declaration is hoisted to the top but the initialization is staying exactly where we type the name variable. This means that we can access the name variable from everywhere in the enclosing block in case of var binding.
To address this issue with var ECMAScript6 introduces block-level declarations using let and const.

7. Push vs Unshift

Both the methods are used to add elements to the array.But the only difference is unshift() method adds the element at the start of the array whereas push() adds the element at the end of the array.

push()
Array.push() method is used to add an element at the end of an array like a queue .In the following example it is shown how to add an element using push() method.

Example:

var cars = [“Benz”, “Lamborghini”, “Tata safari”];
cars.push(“Ferrari”);
console.log(cars);

unshift()
Array.unshift() method is used to add an element at the starting of an array.
var cars = [“Benz”, “Lamborghini”, “Tata safari”];
cars.unshift(“Ferrari”);
console.log(cars);

8.Pop Vs shift

Shift() and pop() methods are used to remove an element from an array. But there is a slight variation between them. The method shift() is used to remove the first element from an array, whereas the pop() method is used to remove the last method from the array. Let’s discuss them in detail.

Shift()
Using this method we can remove the first element of an array.

var companies = [“Tesla”, “Spacex”, “Hyperloop”, “Solarcity”];
console.log(“Before shift:” +” “+ companies );
companies .shift();
console.log(“After shift:” +” “+ companies );

Pop()
In the following example, there are 4 elements in the array. When the pop() method is used the last element was removed and the remaining array is displayed as shown in the output.

var companies = [“Tesla”, “Spacex”, “Hyperloop”, “Solarcity”];
console.log(“Before pop:” +” “+ companies );
companies .pop();
console.log(“After pop:” +” “+ companies );

9.JavaScript Reduce

The reduce() method executes a reducer function that we provide on each element of the array, resulting in single output value.

Example:

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

10. React’s Reconciliation

Reconciliation is the process through which React updates the DOM. When a component’s state changes, React has to calculate if it is necessary to update the DOM. It does this by creating a virtual DOM and comparing it with the current DOM.

--

--