You Don’t know JavaScript.

If you are a beginner in JavaScript,you may find something interesting in this tutorial. So let’s get started….

1)(==) Vs (===):

The equality operator(==) coerces of types in order to compare them. That’s why it is widely regarded as bad practice. To solve this problem JavaScript introduced a new operator called Strict Equality operator (===) that does not perform type coercion between its operands.

if(0 ==”0"){console.log(“true”);}else{console.log(“false”);}//output: trueif(0 ===”0"){console.log(“true”);}else{console.log(“false”);}//output: false

2) typeof operator:

It uses to check the type of an object. Let’s see an example —

console.log(typeof(1.2));
//output:Number

But in order to check the type of an object, it is highly recommended to use Object.prototype.toString.

3) instanceof operator:

The instanceof operator is used to deal with custom made objects that originate from the same JavaScript context like as typeof.It shows unexpected results for native types.

//instanceofconst Foo =()=>{}const Bar =()=>{}Bar.prototype = new Foo();new Bar() instanceof Bar; // truenew Bar() instanceof Foo; // true

here Bar.prototype sets as a function object of Foo, but not to an actual instance of Foo. Take a look below —

Bar.prototype = Foo;new Bar() instanceof Foo; // false

4) Constructors of Built-In Types:

The constructors of the built in types like Number and String behave differently when being used with the new keyword and without it.

new Number(10) === 10; // False, Object and Number
Number(10) === 10; // True, Number and Number
new Number(10) + 0 === 10; // True, due to implicit conversion

5) Why Not to Use eval:

The eval function will execute a string of JavaScript code in the local scope and execute any type of code given to it that’s why it is alarming fact for security and performance. Apart from that it only executes in the local scope when it is being called directly and when the name of the called function is actually eval. For instance —

let number = 1;function test() {    let number = 2;    let copyOfEval = eval;    copyOfEval('number = 3');    return number;}test(); // 2number; // 3

6) undefined and null:

Undefined is a type with exactly one value: undefined.

Here are some examples of when the value undefined is returned:

i) Accessing the (unmodified) global variable undefined.

ii) Accessing a declared but not yet initialized variable.

iii) Implicit returns of functions due to missing return statements.

iv) return statements that do not explicitly return anything

v) Lookups of non-existent properties.

vi) Function parameters that do not have any explicit value passed

vii) Anything that has been set to the value of undefined.

viii) Any expression in the form of void(expression)

There are two way to handle the changes to the value of undefined. Here are these —

// 1)As a local variablevar undefined = 123;(function(something, foo) {var undefined;})(‘Hello World’, 42);//2) As a global variablevar undefined = 123;(function(something, foo, undefined) {})(‘Hello World’, 42);

Uses of null:

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.

7) setInterval():

While setTimeout only runs the function once, setInterval — as the name suggests — will execute the function every X milliseconds, but its use is discouraged.

When code that is being executed blocks the timeout call, setInterval will still issue more calls to the specified function. This can, especially with small intervals, result in function calls stacking up.

function foo(){// something that blocks for 1 second}setInterval(foo, 100);

In the above code, foo will get called once and will then block for one second.

8) Delete Operator:

In JavaScript, It’s impossible to delete global variable, function and other constants due to have a DontDelete attribute set. But explicit properties can be deleted easily by using delete operator.

Normal function args also has build in DontDelete attributes. But in case of host objects,it shows unpredictable result.

// global variable:var a = 1; // DontDelete is setdelete a; // falsea; // 1// normal function:function f() {} // DontDelete is setdelete f; // falsetypeof f; // “function”// reassigning doesn’t help:f = 1;delete f; // falsef; // 1// explicitly set property:var obj = {x: 1};obj.y = 2;delete obj.x; // truedelete obj.y; // trueobj.x; // undefinedobj.y; // undefined

9) Hoisting:

JavaScript hoists declarations. This means that both var statements and function declarations will be moved to the top of their enclosing scope.

let number = 1;
bar();
const bar = function() {};const someValue = 42;test();function test(data) {}

10) Name Resolution Order:

All scopes in JavaScript, whatever global or local scope, have a special name this, which refers to the current object. In order to function, arguments refers the arguments that were pass to the function.

For example, when trying to access a variable named foo inside the scope of a function, JavaScript will look up the name in the following order:

i) In case there is a var foo statement in the current scope, use that.

ii) If one of the function parameters is named foo, use that.

iii) If the function itself is called foo, use that.

iv) Go to the next outer scope, and start with #1 again.

That’s enough for today.See you later.Allah Hafiz.

Front-end web developer