Most important JavaScript interview Question.
--
What is truthy and falsy value in javascript?
In javascript truthy and falsy values are:
Truthy values
“..”‘0’[]{}‘false’
Falsy values
0“”undefinednullNaNfalse
What is the difference between double equal (==) or triple equal (===)
In javascript language double equal check only value but triple equal check value and type.
What does ‘this’ keyword mean?
You might be tempted to answer that this points to the instance of the class inside its body, but this is also not right. First off, classes in JS are syntactic sugar and do not introduce any new features. ‘This’ keyword is available in any function and points to the object that contains this function. It might be easier to understand it with an example:
What is a constructor function?
Constructor functions in JS are also not class-related functions and are tied closely to ‘this ’ keyword. A constructor function is called with “New” keyword and returns whatever the value of “this” is. Note that in constructor functions ‘this ’does not point to the outer object, but instead used as a placeholder object:
NaN === NaN?
False. This is an endless source of debate and one of the most confusing parts about JS. In a nutshell, NaN
stands for Not a Number, and just because one value is not a number and another one is not a number does not imply they are equal. The downside is you cannot really check if a variable is NaN
using myVariable === NaN
. You can use the Number.isNaN
function or myVariable !== myVariable
to check for it.
0.1 + 0.2 === 0.3?
False. This trick does not apply only to JS: it is common among floating-point operations in any language. It has to do with the way the CPU processes floating-point numbers. The actual value of 0.1 + 0.2
will be something like 0.300000001
and to check for equality you would write Math.abs(0.3 - (0.2 + 0.1)) <= EPS
, where EPS is an arbitrary small value (0.00001, for example).
What is “strict” mode?
In JS, you enable strict mode by putting "use strict";
at the beginning of the file. Strict mode enables more rigorous error-checking in your code and makes debugging easier. For example, this snippet will work in regular JS, but not strict:
What are the primitive data types in JS?
A primitive data type in JS is data that is not an object and that has no methods. Here is the list of primitive data types in JS:
- Boolean
- Null
- Undefined
- Number
- BigInt
- String
- Symbol
What is the output of this code?
undefined
. This is happening because JS will inject a semicolon after `return` on line 2, and treat lines 3-5 as a scope instead of an object definition.
How do you convert a string to an int?
parseInt()
converts a string to a whole number
Example;
parseInt("30", 10) // 30
parseInt("55px", 10) // 50
parseInt(2.55, 10) // 2
parseFloat()
converts a string to a point number (with a decimal)
Example:
parseFloat("30") // 30
parseFloat("55px") // 50
parseFloat(2.55) // 2.55
You could also accept
Number()
converts a string to a number. This can be a whole number or point number. These can often be less safe than using parseInt
or parseFloat