Hello, I am Roky. I am here to share some common JavaScript interview questions. So, let’s start.
Null Vs Undefined
Sometimes, we can get ‘null’ or ‘undefined’ after printing some variables in the console. It is very disgusting. We all want to handle them lest they should come.
When we declare a variable and don’t set a value to the variable, then the value of the variable will be ‘undefined’. But it is not possible to auto set a ‘null’ value to a variable. We have to manually set ‘null’ to a value.
const a;
console.log(a); // undefinedconst b = null;
console.log(b) // null
Scope
Scope in JavaScript means that in which area of code a variable is accessible. If we declare a variable under a curly bracket or a block, we cannot access the variable outside of the block. It is called the local scope. But if we declare a variable outside of all block or curly bracket, then it is called global scope.
When we want to access a global variable from outside of the block, then it will throw an error.
const scopeExample = () => {
const a = 5;
console.log(a) // 5
}console.log(a) // It will throw an error.
If there is a variable outside named ‘a’ and it is defined, then it will show an output.
const a = 10;const scopeExample = () => {
const a = 5;
console.log(a) // 5
}console.log(a) // 10
Double Equal (==) vs Triple Equal (===)
Double equal doesn’t consider what types are the variable. If we compare a number with a string, it will not consider the type. See the example:
console.log(5 == '5'); // true
Triple equal considers what types are the variable. If we compare a number with a string, it will consider the type. If the data type doesn’t match, it will return false. See the example:
console.log(5 === '5'); // false
Recussion
It is a technique by which we can call a function from that function. It has two conditions stage. In the main stage, we will check the condition and calls it again and again. In the final stage, the function doesn’t call itself and ends the program.
function recursion(x) {
if(x < 10) {
return x;
}else {
return x * recursion(x-1)
}
}const a = recursion(15);
console.log(a) // 32432400
When the input value is lower than 10 then it returns the input variable. Otherwise, it calls it’s again with the less one number. After all, the result of recursion(15) is 32432400.
Bind
Bind is a JavaScript technique in which we can get and use an object’s function to another object.
const firstObject = {
name: 'Roky Das',
firstFunction: function() {
console.log('Hi I am from function');
}
}const secondObject = {
name: 'Mintu Das'
}const secondFunction = firstObject.firstFunction.bind(secondObject);
secondFunction(); // Hi I am from function
Here, in secondObject, there is no function named ‘secondFunction’. But using bind method, we take the firstFunciton to use for secondObject.
Global Variable
Global variable is that variable that can be accessible from anywhere of that code. When we have some variable that is necessary to access from any block, we should declare it as a global variable.
var myName= "Roky Das";const showName = () => {
console.log(myName); // 'Roky Das'
}
Window
When we browse on our browser, that is shown all are in the window. We use document objects to access all elements and this document is also a part of this window object.
There are many functions in the window. They are to handle the window object in a browser.
Clousure
The closure is a technique, using that we can access the outer loop variable calling outer function and calling the inner function from the outer function. Normally, we cannot access a variable of a function from another function. But using the Closure method, we can access the parent function’s variable from children's function.
function outer() {
var name = 'Roky Das';
function showName() {
console.log(name);
}
showName();
}
outer();
Implicit Conversion
If we multiply with a string consist of only digits, normally we can think that there will be an error. But it will return something. It is called an implicit conversion. See the example:
const a = 20 * "3";
console.log(a) // 60
Here 3 from the string “3” is multiplied with 20 and return 20 * 3 = 60.
Array objects
We can convert an array to a string using toString() method. See the examples:
[34, 56, 87].toString() // "34,56,87"