10 interesting interview problem-solving code on basic javascript
- Truthy and falsy value:
The true and false value in javascript is something different from another programming language. There are some way we can identify the true and false value in javascript.
- You can write a condition like this using if else.
const age = 4;if (age >= 4) {console.log(‘condition is true’); } else {console.log(‘condition is false’);}
This code will return simply
// condition is true
It is same for all programming language.
But if you don’t fulfill the condition in if else. Javascript will works like -
Where javascript works as false with 0 .
Like 0 javascript will works with undefined,empty string, NaN , null,false as falsy value.
And the truth value is : “0”, [], {}, any number without 0, true etc
2. Undefined vs null:
Undefined in javascript means you don’t defined any variable or function .
- Example 1 :
- Example 2: If you don’t return a function then it will give you a undefined. like -
- Example 3: If you don’t pass any parameter then it will give you undefined.
like -
Null means not existance.
3. double equal (==) vs triple equal (===) :
In javascript double equal don’t check the type of value . But triple equal checks the type. In this given example you will understand it -
So it can be said that double equal only check the value not type but tripple equal will check both
4. Scope, block scope, access outer scope variable:
Scope means area of any variable or function. If a variable always works in a function it will not be console logged outer the function. We can able to console log the variable within the function only.\
In this code when I am tried to call result outer the function it gives an error. But it works fine within the function.
Again if you set a variable outer a function it will be accessed from anywhere. like -
Where I have set the number variable in outer space so it is possible to call it in inner space in function.Which is called actually global scope.
5. Closure, encapsulation, private variable :
If we set a function within another function it will create a close environment which called closure. like this -
Here stopWatch is the main function. Here count is a variable in this function . This function returns a another function named increase(). The increase function always increase the value of count.
When stopWatch function is called from outside with a variable clock1. Then the increase function set a specific value for a specific variable. This functionality is called cousers.