Skip to main content

Posts

Showing posts with the label variable

Javascript Interview Question and Answer - 3

In this post, I have discussed 5 basic javascript question and answer that is mostly asked in the interviews. Most of the time interviewer starts the interview with some of these basic questions.
So here we have

Q1. What is the difference between == and ===?

Solution : When we want to test the equality of two values(LHS and RHS), we use == or ===.
== is basically used to check if the two values are equal or not,and we are not considering their datatype. e.g.
if we check

5 == "5"   // true as values are equal
undefined = null   //  true as both represents NO VALUE

In case of ===, we not only consider the values but the datatype as well.e.g.

5 === "5"   // false as though values are equal but datatypes are different

Similarly

undefined === null  //  false, as datatype of undefined and null is undefined and object respectively

It is highly recommended to use === in the programming as we get very unexpected results out of ==. e.g.

false == 0   // true
false == "false&qu…

What is hoisting in Javascript

One of the most important and little confusing topic in Javascript is Hoisting.

The literal meaning of the word 'hoisting' is 'raising up' and its meaning w.r.t Javascript also remains the same.
Before the execution of Javascript program/file/function, it is parsed by the Javascript engine(e.g. V8 engine in Chrome). During this parsing phase, all the declaration(variable and function) gets hoisted. This mean the memory is allocatted for these variables and functions during the parsing phase.
Basically its a default behaviour of Javascript engine to move all the declaration to the top of that function scope in which it is defined.
Variable hoisting Since variable declaration are moved on top of the function scope, we don't get error(but undefined value) when we try to fetch a variable before its declaration. e.g.
1.   console.log(num);    // undefined
2.   var num = 5;
Here, num defined in line 2 but because of hoisting its declaration is moved on top by Javascript e…

Difference between undefined and null

Today we are gonna talk about the famous question in Javascript which all of us must have faced in interview.

The difference between undefined and null.

But before we talk about their differences, lets first think, why are we comparing undefined and null and why not undefined and string or string and null?

So there must be something common,in general, between undefined and null. And that commonality is both of have them represents NO VALUE for the variable.

You can also call this NO VALUE as void or empty.

So basically when we say as variable is undefined or null that mean the value doesn't exist for the variable. Lets see how is it so.

1.   undefined == null     // true, so the value is same

Now lets check each one of them.First lets talk about undefined.

When do we get this undefined? Generally when we declare a variable but doesn't initialise it we get undefined. e.g

1.   var foo;
2.   console.log(foo);   // undefined

Also when we try to access a variable before its initialis…

Difference between var, let and const

var, let and const in Javascript
 Variable can be declared in three ways: varletconstvar variables are function-wide scoped. Let's understand this with an example,    if(true){
      var x = 5;    // variable declared with "var"         console.log(x);  // x will be 5
   }
 console.log(x); // even here, x will be 5 
So in the last example we can see variable "x" is available outside the "if" block, that means the scope of variable with "var" is not block scope.
let see with another example,
1.  function helloWorld(){      var x = 5;
2.     function abc(){
3.        console.log(x);      // even x will be 5
4.     }
5.     abc();
6.     console.log(x);  // x will be 5
7. }
8. helloWorld(); So in this example, we can see the variable "x" is available throughout the function in which it is declared.
One of the famous example/question discussed in the interviews is following
1.       for(var i=0;i<5;i++){
2.                console.log(i);
3.       }
4.…