Wandering in JavaScript Garden
1)What is instanceof operator?
The instanceof operator allows to check whether an object belongs to a certain class. It also takes inheritance into account.Such a check may be necessary in many cases.t returns true if obj belongs to the class or a class inheriting from it.The syntax is
obj instanceof Class
2)Difference between == and === in JavaScript.
Since JavaScript supports both strict equal operator(===) and type converting equality(==) it’s important to know which operator is used for which operation.When we compare two variables of different type e.g. a boolean with a string or a number with String using == operator, it automatically converts one type into another and return value based upon content equality, while === operator is strict equality operator in Java, and only return true if both variable of same type and also contains same value. This will be much clear with following example of == and === operator in JavaScript :
0==false // true, because false is equivalent of 0
0===false // false, because both operands are of different type
2==”2" // true, auto type coercion, string converted into number
2===”2" // false, since both operands are not of same type
3) Use of the strict equal operator(===)
Casting to a String: By prepending an empty string, a value can easily be cast to a string.
'' + 10 === '10'; // true
Casting to a number: Using the unary plus operator, it is possible to cast to a number.
+'10' === 10; // true
Casting to a boolean: By using the not operator twice, a value can be converted to a boolean.
!!'foo'; // true
!!''; // false
!!'0'; // true
4)What is eval() function? why not to use eval()?
eval() function: The eval()
function evaluates JavaScript code represented as a string.The argument of the eval()
function is a string. If the string represents an expression, eval()
evaluates the expression. If the argument represents one or more JavaScript statements, eval()
evaluates the statements. Do not call eval()
to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.If the argument of eval()
is not a string, eval()
returns the argument unchanged.
why not use: A third-party code can see the scope in which eval()
was invoked, which can lead to possible attacks in ways to which the similar Function
is not susceptible.
eval
only executes in the local scope when it is being called directly and when the name of the called function is actually eval
.
var number = 1;
function test() {
var number = 2;
var copyOfEval = eval;
copyOfEval('number = 3');
return number;
}
test(); // 2
number; // 3
when eval() is in disguise The timeout function setTimeout
and setInterval
can both take a string as their first argument. This string will always get executed in the global scope since eval
is not being called directly in that case.
5)undefined and null
undefined
is a type with exactly one value: undefined and it is a global variable.this variable is neither a constant nor a keyword of the language. This means that its value can be easily overwritten. Some examples of when the value undefined
is returned:
- Accessing the (unmodified) global variable
undefined
. - Accessing a declared but not yet initialized variable.
- Implicit returns of functions due to missing
return
statements. return
statements that do not explicitly return anything
The value null
represents the intentional absence of any object value.It is used in some JavaScript internals (like declaring the end of the prototype chain by setting Foo.prototype = null
), but in almost all cases, it can be replaced by undefined
.
6)The delete operator
If a variable or a function defined in global or functional scope ,then it has a property named either activation object or the Global object. Such properties have a set of attributes, one of which is DontDelete. Variable and function declarations in global and function code always create properties with DontDelete, and therefore cannot be deleted.
// global variable:
var a = 1; // DontDelete is set
delete a; // false
a; // 1
// normal function:
function f() {} // DontDelete is set
delete f; // false
typeof f; // "function"
Explicitly set properties can be deleted normally.In the example above, obj.x
and obj.y
can be deleted because they have no DontDelete
attribute. That's why the example below works too.
var obj = {x: 1};
obj.y = 2;
delete obj.x; // true
delete obj.y; // true
obj.x; // undefined
obj.y; // undefined
7)Discuss about setTimeout()
The setTimeout()
is a method of the window
object. The setTimeout()
sets a timer and executes a callback function after the timer expires.
let timeoutID = setTimeout(cb [,delay], arg1, arg2,…);
In this syntax:
cb
is a callback function to be executed after the timer expires.delay
is the time in milliseconds that the timer should wait before executing the callback function. If you omit it, thedelay
defaults to 0.arg1
,arg2
, … are arguments passed to thecb
callback function.
The timeoutID can be used to cancel timeout by passing it to the clearTimeout()
method.
8) Discuss about setInterval()
The setInterval()
is a method of the window
object. The setInterval()
repeatedly calls a function with a fixed delay between each call.
9) Scopes and Namespaces
Scope: Scope in JS, determines the accessibility of variables and functions at various parts in one’s code.
In general terms, the scope will let us know at a given part of code, what are the variables and functions that we can or cannot access.
There are three types of scopes in JS:
- Global Scope
- Local or Function Scope
- Block Scope
function test() { // a scope
for(var i = 0; i < 10; i++) { // not a scope
// count
}
console.log(i); // 10
}
Namespace: There are no distinct namespaces in JavaScript, which means that everything gets defined in one globally shared namespace.A common problem associated with having only one global namespace is the likelihood of running into problems where variable names clash. In JavaScript, this problem can easily be avoided with the help of anonymous wrappers.
(function() {
// a self contained "namespace"
window.foo = function() {
// an exposed closure
};
})(); // execute the function immediately//syntax description( // evaluate the function inside the parentheses
function() {}
) // and return the function object
() // call the result of the evaluation
10)What is Hoisting?
When you execute a piece of JavaScript code, the JavaScript engine creates the global execution context.The global execution context has two phases: creation and execution.During the creation phase, the JavaScript engine moves the variable and function declarations to the top of your code. This feature is known as hoisting in JavaScript.This means that both var
statements and function
declarations will be moved to the top of their enclosing scope.
console.log(counter); // undefined
var counter = 1;
However, the first line of code doesn’t cause an error because the JavaScript engine moves the variable declaration to the top of the script.