Confusing Thing In JavaScript

HrDelwar Pro
3 min readMay 8, 2021

--

The most popular reason for calling Javascript confusing is its implicit typecasting [changing data type of variables by itself] and its asynchronous nature. Here is discus some JavaScript confusing things.

1. Truthy and falsy

The truthy value is a value that is considered true when it’s in a boolean That’s mean each value also has an inherent boolean value, generally known as either truthy or falsy.

The following values are always falsy:

  • false
  • 0 (zero)
  • ‘’ or “”(empty string)
  • null
  • undefined
  • NaN

Everything else is truthy:

  • ‘0’ (a string containing a single zero)
  • “false” (a string containing the text “false”)
  • [] (an empty array)
  • {} (an empty object)
  • function() {} (an “empty” function)

2. Double equal vs triple equal

JavaScript behaveour is loose equality comparisons with double equal (==) and strict equality comparisons with triple equal(===). JavaScript using == only makes sure that they are equal in value. Not by type. In JavaScript (“5” == 5) is true because its check only value not check type, but (“5″ === 5) is false because the second five is an integer and the first was a string type.

3. Scope

In JavaScript there are two types of scope

  • Local scope
  • Global scope

JavaScript has function scope. Each function creates a new scope. Variables declared within a JavaScript function, become local to the function. Local variables have function scope, they can only be accessed from within the function. A variable declared outside a function, becomes global.

4. Hoisting

Moving declarations to the top is hoisting . It is JavaScript default behavour. In JavaScript, a variable can be used before it has been declared.

x = 5; // Assign 5 to x
var x; // Declare x

Using a let variable before it is declared will give a Reference error. Using a const variable before it is declared, is a syntax errror. Because variables defined with let and const are hoisted to the top of the block, but not Initialized.

5. “this” keyword

How “this” keyword work , why it’s work that’s undestanding is tricky. But there is some background rules:

  • “this” always refers to an object.
  • “this” refers to an object which calls the function it contains.
  • In the global context “this” refers to window object.

In an object method, “this” refers to the “owner” of the method.

6. Call, bind, apply.

In JavaScript, everything is an object. call(), bind() and apply() is JavaScript methods. call() invokes the function and allows you to pass in arguments one by one. apply() invokes the function and allows you to pass in arguments as an array. bind() returns a new function, allowing you to pass in a this array and any number of arguments.

7. Closure

A closure is a function having access to the parent scope, even after the parent function has closed. JavaScript variables can be local or global scope. In closure sitution global variables behavour like local variables.

8 . “new” keyword

The new keyword is for creating new object instances. It makes the this variable point to the newly created object.

9. Callback function

JavaScript functions are executed in a sequence they are called. A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.

10. Recursion

Recursion is a process of calling itself. A function that calls itself is called a recursive function. A recursive function must have a condition to stop calling itself. Otherwise, the function is called indefinitely.

--

--