Some topic we should know before facing Interview
Difference between Undefined and Null
1. Undefined:If a function does not return any value then it gives undefined output on the function return but what it will return did not mention.
In that case it will also give undefined output.
2.If we don’t set a parameter value each it will also give undefined output.
3.If we don’t set a value then it will be undefined.
4.If we want to see an object property as output, but we didn’t even set the property in the object.It will give undefined.
5.If we set a variable value undefined. It will return output undefined.
Null:It means not existing.Null usually we set as default value or the value it has but now it has not.
Difference between Double equal and Triple equal
If we use double equal in a code, it will only check if the value is ok or not.
But triple===equal not only check the value but also checks the type of value.
Difference Call and Apply Method
#
var obj={num:3};
var addToThis=function(a){
return num+a;
};
console.log(addToThis.call(obj,3)); //6
#
var obj={num:3};
var addToThis=function(a,b,c){
return num+a+b+c;
};
var arr=[1,2,3]
console.log(addToThis.apply(obj,arr)); //9
That’s what the call does it attaches the function to the obj temporarily and then runs it and gives the result back as if it’s a part of the obj itself.
But in apply we don’t have to pass all the arguments,we can combine all the arguments in an array.That’s the only difference between apply.
But bind is a little different.Bind gives us a function.Which has ability to execute.
Scope in Javascript
Global scope
Any variable declared outside of a function belongs to the global scope,and is therefore accessible from anywhere in your code, like we can access it in function,loop in any kind of block we can access
Local Scope
Each function has its own scope, and any variable is declared within that function is only accessible from that function and any nested functions. Because local scope(function scope) in JavaScript is created by functions, its also called function scope.
How to Calculate Factorial of a number using for loop
var factorial =1;
for (var i=1 ; i<=5 ; i++){
factorial = factorial * i;
console.log(“Result=” ,factorial);
} // Result= 120
How to Fibonacci Series using a for loop
var fibo =[0,1];
for (var i=2 ; i<=10 ; i++){
fibo[i] = fibo[i-1] + fibo[i-2];
}
console.log(fibo); // [0,1,1,2,3,5,8,13,21,34,55]
How to Fibonacci series in a recursive way
var fibo =[0,1];
function fibonacci(n){
if(n == 0) {
return [0];
}
else if(n == 1) {
return [0, 1];
}
else {
var fibo =fibonacci(n -1);
var nextElement = fibo[i-1] + fibo[i-2]
fibo.push(nextElement);
return fibo;
}
}
Var result = fibonacci(10);
console.log(result ); // [0,1,1,2,3,5,8,13,21,34,55]
How to Check whether a number is a Prime Number or not
var n =8;
for (var i=2 ; i< n ; i++){
if(n%i ==0) {
console.log(‘Not a prime number);
break;
}
else {
console.log(‘This is prime number);
}
} //Not a prime number
}
console.log(fibo); // [0,1,1,2,3,5,8,13,21,34,55]