Professional Documents
Culture Documents
the BigInt 0n
the keyword null
the keyword undefined
the boolean false
the number NaN
const o2 = Object.create(null);
// the first parameter is the prototype of the object "o2" which in this
// case will be null specifying we don't want any prototype
console.log(o2.toString());
// throws an error o2.toString is not a function
39. Why does b in this code become a global variable when you call this function?
function myFunc(){
let a = b = 0;
}
myFunc();
The reason for this is that assignment operator or = has right-to-left associativity or evaluation.
What this means is that when multiple assignment operators appear in a single expression they
evaluated from right to left. So our code becomes likes this.
function myFunc(){
let a = (b = 0);
}
myFunc();
First, the expression b = 0 evaluated and in this example b is not declared. So, The JS Engine
makes a global variable b outside this function after that the return value of the expression b = 0
would be 0 and it's assigned to the new local variable a with a let keyword.
We can solve this problem by declaring the variables first before assigning them with value.
function myFunc() {
let a,b;
a = b = 0;
}
myFunc();
42. What's the difference between var, let and const keywords?
var declarations are globally scoped or function scoped while let and const are block
scoped. var variables can be updated and re-declared within its scope; let variables can be
updated but not re-declared; const variables can neither be updated nor re-declared. They are
all hoisted to the top of their scope
43. What are Arrow functions?
An arrow function expression is a compact alternative to a traditional function expression, but is
limited and can't be used in all situations.
Does not have its own bindings to this or super, and should not be used as methods.
Does not have arguments, or new.target keywords.
Not suitable for call, apply and bind methods, which generally rely on establishing
a scope.
Can not be used as constructors.
Can not use yield, within its body.
Template literals can contain placeholders. These are indicated by the dollar sign and curly braces
(${expression}). The expressions in the placeholders and the text between the backticks (` `) get
passed to a function.
The default function just concatenates the parts into a single string. If there is an expression
preceding the template literal (tag here), this is called a tagged template. In that case, the tag
expression (usually a function) gets called with the template literal, which you can then
manipulate before outputting.
To escape a backtick in a template literal, put a backslash (\) before the backtick.
1. “ ”
2. 0
3. -0
4. undefined
5. null
6. NaN
Explicit Coercion
In this case, type conversion is explicitly done in the code by the developer. JavaScript provides
Converting to Number
The Number() global method is used to convert any other data type value to numeric values.
Converting to String
The String() global method is used to convert any other data type value to string values.
Converting to Boolean
The Boolean() global method is used to convert any other data type value to Boolean values.
56. What is NaN? and How to check if a value is NaN?
isNaN() method determines whether a value is NaN (Not-A-Number). This method returns
true if the value is of the type Number, and equates to NaN. Otherwise it returns false.
57. How to check if a value is an Array?
In JavaScript, we can check if a variable is an array by using 3 methods, using the isArray
method, using the instanceof operator and using checking the constructor type if it matches
an Array object.
Method 1: Using the isArray method
The Array.isArray() method checks whether the passed variable is an Array object.
Method 2: Using the instanceof operator
The instanceof operator is used to test whether the prototype property of a constructor appears
anywhere in the prototype chain of an object. This can be used to evaluate if an the given
variable has a prototype of ‘Array’.
Method 3: Checking the constructor property of the variable
Another method to check a variable is an array is by checking it’s constructor with Array.
58. How to check if a number is even without using the % or modulo operator?
Method 1: By using the bitwise (&) operator, a number can be checked if it is odd or even.
Method 2: By multiplying and dividing the number by 2. Divide the number by 2 and multiply it
by 2. If the result is the same as that of the input, then it is an even number otherwise, it is an odd
number.
Method 3: By switching temporary variable n times with the initial value of the variable being
true.If the flag variable gets its original value (which is true) back, then n is even. Else, n is false.
59. How to check if a certain property exists in an object?
JavaScript provides you with three common ways to check if a property exists in an object:
63. What's the difference between the in operator and the hasOwnProperty method in objects?
he key difference is that in will return true for inherited properties,
whereas hasOwnProperty() will return false for inherited properties
64. What are the ways to deal with Asynchronous Code in JavaScript?
async keyword placed before a function and the keyword await makes JavaScript wait until
that promise settles and returns its result. alert(result); // "done!" f(); Prepending
the async keyword to any function means that the function will return a promise and wraps
non-promises in it.
65. What's the difference between a function expression and function declaration?
The main difference between a function expression and a function declaration
is the function name, which can be omitted in function expressions to create
anonymous functions. A function expression can be used as an IIFE (Immediately
Invoked Function Expression) which runs as soon as it is defined.
66. How many ways can a function be invoked?
4 ways:
1. Invoking functions as functions
2.Invoking functions as object methods
3. Invoking functions as constructors: using the new keyword
4. Invoking function with apply() and call() methods