Top Ten JavaScript Interview Questions for Freshers
1. Null vs Undefined:
In Javascript null is assigned value, which means this value doesn't exist. Without assigning a value of null, you can not get from anywhere by Javascript a Null result.
var assign = null;console.log(assign); //==> null
‘Null’ result can get only by assigning a variable with ‘null ’ value, which is shown in the example. Javascript Programmer uses this value for different purposes.
In JavaScript Undefined means previously a variable is declared by a programmer but he/she never assigned any value there. We can get Undefined results in many cases.
var taka;
console.log(taka) //==> undifined
There's several ways you can get undefined;
var object = { name: 'Ria Akter' };
console.log(object.cellphone); // ==> undefinedvar array = ['str1', 'str2'];
console.log(array[3]); // ==> undefinedvar gf = undefined
console.log(gf) // ==> undefined
2. equal (==) vs triple equal (===)
Both are comparison extensions in JavaScript but their comparison method is different. Double equal compare only the “value ” wheres triple equal compare both value and type.
const foo = "test"
const bar = "test"
console.log(foo == bar) //true {value same}
console.log(foo === bar) //true {value same type same}const foo = null
const bar = "null"
console.log(foo == bar) //true {value same}
console.log(foo === bar) //false {value same but type are not}
3 /4/ 5. hoisting, block scope, global variable
We know that there are three types of expression in javascript for declaring a variable: Let, Const & Var.
Hoisting means lifting something. var is the only expression that is hoisted to the upper level from the nested area to the parent.
console.log(x)function parent(){
function child (){
var x = 9
}
}
Here I console.log variable x which is nested in the child function. But it gives you a result escaping error. Because variable x is hoisted here. Javascript hoists the variable “x” to the top but it does not hoist its value. As a result, you get “Undefined” result here.
In the case of Let & Const, the variable doesn't hoist.
console.log(x)function parent(){
function child (){
let x = 9
}
}
// ==> errorconsole.log(x)function parent(){
function child (){
const x = 9
}
}
// ==> error
That means “Let” and “Const” variables create block scope inside curly-bracket{}.
A global variable is declared in the window, outside of the function. We can declare a global variable inside a function by following
function gVariable() {
window.foo = 10 //Global Variable
}
6. Understanding ‘this’
“this” references the object that is currently calling the function. If a function is called with new, this is the object created by the function. If a function called with “call”, “apply”, or “bind”, this refers to the object is passed in the argument. If a function is called with a contextual object, this refers to the contextual object.
7. DOM (document object model):
DOM represents the node tree of all the UI elements present on a web page. If any element on the user interface changes, the DOM updates itself with the new changes.
8. JavaScript callback function:
Callback function is the use of a function as a parameter in another function. We generally used callback function for asynchronous execution & for even handling purposes. Its makes your code clean and DRY (do not repeat yourself).
Here is an example of callback function,
function greetings(string, cbFunction) {
cbFunction(string)
}
function cbFunction(string) {
console.log("Good Morning", string);
}
greetings("Ridwan", cbFunction) // Good Morning Ridwan
here cbFunction is acting as a callback function.
9. bind, call and apply:
Trice are used to collaborate or get a method situated in another objects in javascript. Those are the ways by which we apply a method in an object to another object.
The “bind” method creates a new function that, when it is called, has its “this” keyword set to the provided value.
The “call” method calls a function with a given “this” value and arguments provided individually separated by using comma(,).
The “apply” method calls a function with a given “this” value and arguments provided an array of all of our parameters.
Remember that the purpose of “call” and “apply” methods are same.
10. API (Application Programming Interface):
API provides properties and methods of data structures to the users easily without any “behind the scenes” works. It is kind of readymade food serving!