JS10–Ten Tricky Javascript Facts We Need to Know for Interview

1. Truthy vs Falsy value :
In javascript, when we do a boolean job, in other words, we expect a value from a boolean context, if the value is considered as true, so the value is a truthy value. All the values are truthy in javascript except which are mentioned as falsy. Simply any variable with a value is truthy. Following are some truthy values :
True
{ } object, whether empty or not
[ ] array, whether empty or not
10 number, whether positive or negative
‘True’ non empty string
‘False’ non empty string
new Date( ) Date object
Infinity etc.
Similarly in a boolean context, if a value is considered as false, that is a falsy value. Following are some falsy values :
false
0 both positive and negative
‘ ‘ empty string
null
undefined
NaN etc.

2. Null vs Undefined
Undefined is a type in javascript, whereas null is an object.
undefined means a variable is declared but no value has been assigned like :
let example ;
console.log(example) // undefined
console.log(typeof example) // undefined

On the other hand, null is an assignment value in javascript. We can assign it to a variable like :
let example = null ;
console.log(example) // null
console.log(typeof example) // null

3. Double equal(==) vs triple equal(===) comparison :
The double(==) and triple equal(===) both are the comparison equality operators in javascript. The triple equal is called a ‘strict equality operator’ and the double equal is called a loose equality operator.
Triple equal compares its right and left side’s value strictly, that’s why it is strict. It compares both value equality and also type equality. If the value and type are the same to each other, it returns true.
Double equal loose because it doesn’t compare type, it only compares the value equality. But behind the scene, it compares the values of both sides with considering the same type. In other words, it tries to convert the type of the values into the same type. After being successful in this conversion, then it compares the value equality.
We see above, triple equal doesn’t convert types, it compares the type, but double equal doesn’t check types, it converts the types of both values into the same type.
We should always use the triple equal for comparison to prevent tons of potential bugs. We shouldn’t use double equal except as a strong use case for it.

4. Scope :
Scope is the current context of a particular execution. Scope refers to the visibility of variables. This is the context in which values of variables and expressions are visible or can be referenced. If a variable of expression is not in the current scope, then it is unavailable for use.
There are two scopes in javascript. They are :
Global Scope : There is only one Global scope in the whole javascript document. All the functions and variables defined in global scope can be accessed and altered in any other scope.
Local Scope : Variables declared inside a function become local to the function. Every function has its own scope.

5. Local Scope — Function scope, Block scope, Lexical scope :
Function scope : Whenever we declare a variable inside a function, the variable is visible only within the function. We can’t access it outside the function. We used var keyword to define variables for a function-scope accessibility.
Block scope : In javascript or other programming languages, whenever we see code inside {…} curly braces, it is a block. The scope inside this block is called block scope. The ES6 literals const and let keywords allow us to declare variables inside the block scope. So, the variables are only accessible inside the corresponding scope.
Lexical scope : Lexical scope means a children scope has the access to the variables defined inside its parent scope. The children functions are lexically bound to the execution context of their parents.

6. Closure :
In javascript, we can create nested functions as many as we need. Closure is a default behavior in javascript. Closures are created every time a function is created at function creation time. Closure gives us access to an outer function’s scope from an inner function. Because closure means that a child function always has access to the vars and parameters of its parent function, even after the parent function has returned or its invocation is over. In some programming languages, the local variables inside a function exist for just the duration of that function’s execution, but in javascript, it doesn’t get destroyed after the returning of corresponding function, it remains accessible from its inner functions. This behavior is called closure.

7. Window Object and Global Variable :
The window object represents the current window of a browser. A window object is created automatically by the browser. Window is the object of the browser not javascript object. In previous, we read about global scope. Variables when declared in a global scope, they become global variables. The window object is the global object in the browser. Any global variables or functions that are declared in the global scope become the properties of the window object. These properties can be accessible from anywhere because they are globally set.

8. this keyword :
Usually the this keyword refers to the object it belongs to. It has different behaviors and values regarding where it is used.
In general or inside a function, this keyword refers to the global object. If this is a browser, it’ll refer to the window object. But in strict mood, this will be undefined. Inside a method, this refers to the parent object. In an event, this refers to the element that received the event.

9. var vs let vs const :
The var keyword is the older javascript approach to declare a variable. ES6 brings to us new keywords to declare variables which are more efficient. They are const and let. We can re-assign the values of variables that have been declared with var and let keyword but we can’t do that in const.
But the important differences among var, let and const are following :
Var declarations are globally or function scoped but let and const are block scoped.
Variables declared with var can be updated and also re-declared even within its scope, let also can be updated but not re-declared. Const variables can neither be updated nor re-declared as they are constant.
Variables declared with these three keywords are all hoisted to the top of their scope. Var variables are initialized with undefined but let and const variables are not initialized.
Var and let variables can be declared without initializing, but const must be initialized during declaration.

10. Callback Function :
A callback function is a function that passed into another function as an argument for that function. This callback function is then invoked inside the outer function to finish the job.
In javascript, functions are objects. We can pass objects to functions as parameters because functions are the first-class citizen in javascript. So easily, we can pass functions as parameters to another functions.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store