JavaScript Tricky Basic Concepts

You need to know these concepts to become a pro of JS.

Truthy vs falsy value

Falsy values are:

0
-0
null
undefined
""

Null vs undefined

typeof null shows object in JS is a mistake.

You can do math operations with null but not with undefined.

How do you get undefined

2. A function doesn’t return a value

3. Don’t pass required parameters to a function

4. Try to access a property of an object that doesn’t exists

== vs ===

1 == ”1” // true

1 === “1” // false

Scope

A variable that is declared in a block (e.g. a for loop) cannot be accessed outside of that block, it is called block scope.

A variable declared in a function is only available inside that function, it is called function scope.

Closure

A closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

An example:

function adder (){
let x = 0;
return function add1 () {
x++;
return x;
}
}
const adder1 = adder ();
adder1(); // 1
adder1(); // 2
const adder2 = adder ();
adder2(); // 1

Call vs Apply

Window

Properties and methods of window object can be accessed directly.

Window.console == console

Window.price = 500; price is now a global variable and can be accessed directly as price.

This

A general rule is look at the left of the (.), if there is nothing then this will represent global object. Else it will represent what is in the left.

myObj.sum(); Here, this keyword in the sum() function will represent myObj.

calculate(); Here, this keyword in the calculate() function will represent global object.

· In a method, this refers to the owner object.

· Alone, this refers to the global object.

· In a function, this refers to the global object.

· In a function, in strict mode, this is undefined.

· In an event, this refers to the element that received the event.

setTimeOut(), setInterval()

JavaScript runtime concept

Heap => memory allocation

Queue => List of tasks to be processed

Event loop => Pushes tasks from queue to stack

Web/C++ APIs => For async tasks

--

--

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