JavaScript Confused Topics and their Answer

Md Rojon Ali
4 min readMay 8, 2021

There are many concepts about JavaScirpt remain always confused. I am trying to explaining some of them as easily as possible.

Photo by Roman Synkevych on Unsplash

1. Truthy and Falsy value

It simple. In JavaScript, a truthy value is a value that is considered true. All values are truthy unless they are defined as falsy.

false, 0, -0, 0n, "", null, undefined, and NaN , these are only falsy values.

There is one more thing I want to mention that except false the value of the others is not exactly false but they are considered as false. So, if you want to check with double equal whether it is true or false it will return as what we are discussed. But with triple equal, it will only look for the actual false or true as boolean.

console.log(false == 0) // true
console.log(false === 0) // false

2. Null vs undefined

When a variable is declared but no value is assigned to it, so what javaScript do automatically it put a placeholder which is undefined.

Where Null means an empty or non-existent value. And, Null is certainly assigned.

They both are falsy value and Null is an object and actually an error.

3. double equal vs triple equal

Equality comparison in javascript there are two types of equality comparison.

  • Abstract Equality Comparison (==)
  • Strict Equality Comparison (===)

Inequality comparison in abstract equality or double equal check for primitives not for the types. But in strict equality or triple equal check for value and also for types.

4. Hoisting

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. It is the definition of hoisting.

So that in JavaScript, a variable can be declared after it has been used.

5. Understand this keyword

THIS is where many of us quit programming. I don’t know is it fun or not but it is a very confusing topic I have seen.

There is something we should look at it:

  • 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.
  • Methods like call(), and apply() can refer this to any object.

6. bind, call, apply

bind, call and apply are methods for Object. We know there is a default behavior of “this” which refers to the owner object. So if we define a method for an object and want to call that method for another object that should refer to that particular object with “this” keyword we can do that with these methods.

There is some difference in use but the result is the same.

where call and apply method directly call that method only apply to take arguments as an array.

bind can be called for later use of that method whenever we want.

7. Scope

There are two types of scoping in JavaScript.

  • Local Scope
  • Global Scope

Concepts are, where you can access your defined variable and where not. Every function creates its new scope. A declared variable in one function can’t be used in another function. In the Lexical scoping concept, you can use a variable declared in the parent function from the child function.

8. Clousers

We know now that we can have access to a variable that is declared in a parent from a child. The interesting part is even if we return the parent function we can still use its variables from the child.

A closure is a combination of a function bundled together (enclosed) with references to its surrounding state. In other words, 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.

9. IFFE

Immediately Invoked Function Expression(IFFE) is a JavaScript function that runs as soon as it is defined.

JavaScript functions can be created either through a function declaration or a function expression. A function declaration is the “normal” way of creating a named function.

The primary reason to use an IIFE is to obtain data privacy. Because JavaScript’s var scopes variables to their containing function, any variables declared within the IIFE cannot be accessed by the outside world.

10. The Module Pattern

In JavaScript, a module is a small unit of independent, reusable code. Modules are the foundation of many JavaScript design patterns and are critically necessary when building any non-trivial JavaScript-based application.

JavaScript doesn’t have private keyword but we can achieve private methods and properties using closures.

We use module patterns for maintainability, reusability, and Namespacing.

These are 10 JavaScript concepts. Keep looking for update.

--

--

Md Rojon Ali
0 Followers

I am a full-stack web developer. Programming is not fun until you are taking it simply. Programming is hard because you are introduced with hard way. Let's rock