JavaScript Tricky Basic Concepts

Truthy vs falsy value

Anything that is not falsy is truthy.

0
-0
null
undefined
""

Null vs undefined

Null is a primitive value that represents absence of value. You get undefined when you declare a variable but don’t assign any value.

How do you get undefined

1. Declare a variable but not assign any value

== vs ===

Both are used to compare. But a == compares only value whereas a === compares both the value and the type.

Scope

A scope is a range of a variable.

Closure

A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.

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

The difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be comma separated. A useful mnemonic is "A for array and C for comma."

Window

The window object represents the running environment. It is global scope. All the global variables of JS are properties and global functions are methods of the window object. The document object is also a property of window.

This

The this keyword represents the context from where it was called.

setTimeOut(), setInterval()

setTimeOut is used to make async call. The time given represents the minimum time to execute the function. After finishing all the sync actions the time out will start.

JavaScript runtime concept

Stack => data structure, LIFO

--

--

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