Some ticky things about Javascript

Md. Al-amin Howlader
4 min readMay 8, 2021

--

1. Truthy and Falsy values:

In javascript, there is a type called boolean. Which have two values true and false. Sometimes it will so confuse to difference them. Generally, maximum values are true, and little is false. let’s look at some falsy values. 0 is mainly a falsy value. if a variable value is 0 then it will a falsy value. Example:

const myNumber = 0;
console.log(myNumber); // false

Like 0, an empty string (“”), null, and NaN are also falsy values. If someone declares a variable but didn’t set any value then its value will be undefined. It is also a falsy value. Example:

const myName;
console.log(myName); // undefined

let’s go to truthy value. If a variable has a value positive or negative no matter it will be a truthy value. An empty string has space like this (“ ”) then it will become a truthy value. Even 0 into a string like that “0” then it will also become a truthy value.

2. Null vs Undefined:

If you need to use an empty value of a variable you can use null. Undefined is a value of those variables which values didn’t assign. If a function didn’t have any return value it will return undefined. If you call a property of an object that didn’t have this property it will also return undefined. See these example:

// 1st example
function double(number) {
const double = number * 2;
}
const result = double(5);
console.log(result); // undefined
// 2nd example
const myObject = { name: Alamin, roll: 436 }
consle.log(myObject.GPA); // undefined

Here we can see in the 1st example double function have no return that why it returns undefined and the 2nd example there is an object with two property name and roll. but I try to console.log GPA property for this reason it also gives undefined.

3. Double (==) vs Triple(===):

This also a confusing item of javascript. But not so much. let’s get it. Double equal compare two values they are equal or not. But they didn’t check their types. Example:

const number1 = 5;
const number2 = '5';
if(number1 == number2) {
console.log('This is true');
} else {
console.log('This is false')
}
// output will be: This is true

Actually, both number values were 5 but one is a number and the other is a string. Double equal compare like this but triple equal also check types. If we run the same example as before using (===) instead of (==) then the output will be: This is false. Because those were not the same in type.

4. Scope of variable:

Scope generally 3 types — 1) Global scope 2) Local scope/function scope 3) Block scope.

  • Global scope: Which variable is declared globally and accessible any place of code that is the global scope. Example:
const value = 55;
if(value) {
console.log(value); // 55
}

Here we can see value variable was accessible into the if block. It will also accessible to any part of the code.

  • Function scope: Every function has an area which is the function scope. If a variable is declared into a function it can’t accessible out of the function. Example:
function squareNumber(number) {
let result = number * number;
}
console.log(result); // referenceError: result not defined

Here we can see result variable was declared into squareNumber function. If we try to access it outside of the function we will get an error. But it will accessible into the function.

  • Block scope: Block scope actually like a function scope. if-else, for, forEach, etc have a scope. Into this scope, any declared variable didn’t accessible outside of the scope. Example:
for(let b = 1; b <= 5; b++) {
let newNumber = b * b + 1;
here we can access b or newNumber
}
// here we can't access b or newNumber

Note: if we declared a variable with a var keyword then the variable will be accessible from anywhere. That means the var keyword always makes global scope.

5. Closure:

It is also a confusing thing about javascript. If a function returns another function and the child function return from the parent function then a closure creates there. In general, closures are created every time when a function was created. Example:

function multiple(number1) {
return function(number2) {
return number1 * number2;
};
}
const multiple3 = multiple(3);
const multiple5 = multiple(5);
const result1 = multiple3(4);
console.log(result1); // 12
const result2 = multiple5(4);
console.log(result2); // 20

Here we can see, result1 and result2 have the same value of parameters but they gave different results because their parent function has different values of parameters.

--

--