These 10 Simple JavaScript Getting Tricks To Complete Your Tasks Quickly And Efficiently

Juned Ahmed
5 min readJun 7, 2021
JavaScript
  1. Truthy and Falsy Values in JavaScript: Truthy and Falsy values in JavaScript are terms we use in JavaScript for those values that are evaluated to be true or false, but in reality, these values are not true or false. So this kind of values play an important role while doing comparisons in JavaScript, and taking care of those logics is really important while writing the program.

i) Falsy Value: Most of the values in javascript are Truthy, so it is better to list the Falsy value where we have only a limited number of cases. There are a total of 8 falsy values in Javascript:undefined

  • NaN
  • null
  • false
  • “” (Empty String)
  • 0 (0 is an alias for +0)
  • -0
  • 0n (BigInt)

We can validate whether the above values are falsy or not by passing them as a parameter to the truthyOrFalsy function.

ii) Truthy Value: Despite we might think that the empty array( [] ) or empty object( {} )should be falsy values, but they are actually truthy values in JavaScript.

2) Null Vs Undefined: The key difference between null and undefined in JavaScript is that null is used to assign a non-value to a variable while undefined is used when a variable is declared but not assigned with a value.

i) Null: JavaScript objects are written using curly braces. Object properties are written as name, value pairs. They are separated by a comma. e.g. var student = {name: “Ann”, marks: 65};

When the programmer wants to assign a non-value to a variable, he can use the data type null. This data type is considered as an object.

Refer the below JavaScript statements.

According to the above, the value of student is null. Data type is object.

ii) Undefined: In JavaScript, when a variable is declared but didn’t assign a value, then it is undefined.

Refer the below JavaScript statements. If there is a statement such as var x; where x is a variable. Then x has a value undefined. The data type is also undefined.

This will display the value on the HTML page. It gives undefined. Therefore, it contains a value of undefined. When writing document.write(type(x)); and reloading the page, it will still give undefined. Therefore, the variable x has a value which is undefined and the type is also undefined.

Refer the below statement as well.

The variable student has an undefined value. The type of that variable is also undefined.

It is also possible to set the variable value to undefined. Refer below statement.

Now the student variable is having undefined value. The type of variable student is also undefined.

3) Double (==) and Triple (===) Equals in Javascript: Javascript supports both supports Triple equal (===) and Double equal (==).

i) Double equal:

Double equal to compares the two value and return true or false based on that. So comparing number with string having the same value will return true.

For example –

It returns true because both value are same. Take another example.

In this comparison first value is string and second value is integer. It return true value, as it typecast the first value to int.

ii) Triple Equals:

Now let’s see how Triple equal (strict comparison) works in this case. Triple equality operator compares values as well as data type.

If you compare “8” with 8 using triple equals to the result will be false. As this “8” represent string values whereas other 8 represents integer.

4) Scope: Scope determines the visibility or accessibility of a variable or other resource in the area of your code.

5) Global Scope: There’s only one Global scope in the JavaScript document. The area outside all the functions is consider the global scope and the variables defined inside the global scope can be accessed and altered in any other scopes.

6) Local Scope: Variables declared inside the functions become Local to the function and are considered in the corresponding local scope. Every Functions has its own scope. Same variable can be used in different functions because they are bound to the respective functions and are not mutual visible.

Local scope can be divided into function scope and block scope. The concept of block scope is introduced in ECMA script 6 (ES6) together with the new ways to declare variables — const and let.

7) Function Scope: Whenever you declare a variable in a function, the variable is visible only within the function. You can’t access it outside the function. var is the keyword to define variable for a function-scope accessibility.

8) Block Scope: A block scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see {curly brackets}, it is a block. In ES6, const and let keywords allow developers to declare variables in the block scope, which means those variables exist only within the corresponding block.

9) Lexical Scope: Another point to mention is the lexical scope. Lexical scope means the children scope have the access to the variables defined in the parent scope. The children functions are lexically bound to the execution context of their parents.

10) Closure: A closure gives us 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. Example:

Thank you!

--

--

Juned Ahmed

I enjoy developing applications using modern technologies. Proficient in JavaScript, Reactjs, Nodejs, Expressjs and MongoDB. Passionate about new technologies.