JavaScript Tutorial

Touhidul islam
4 min readNov 5, 2020

Truthy and Falsy Values in Javascript

When a value is truthy in Javascript, it does not means that the value is equal to true but it means that the value coerces to true when evaluated in a boolean context.

function truthyOrFalsy(value){
if(value){
console.log("Truthy Value");
} else {
console.log("Falsy Value");
}
}

The above function evaluates the passed value in a Boolean Context(if condition) and checks if whether the passed value is truthy or falsy.

Falsy Values

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 truth Or Falsy function we defined at the starting of this article.

truthyOrFalsy(undefined); // Falsy Value 
truthyOrFalsy(NaN); // Falsy Value
truthyOrFalsy(null) // Falsy Value
truthyOrFalsy(""); // Falsy Value
truthyOrFalsy(false) // Falsy Value
truthyOrFalsy(0); // Falsy Value
truthyOrFalsy(-0); // Falsy Value
truthyOrFalsy(0n); // Falsy Value

Truthy Values

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

truthyOrFalsy([]);  // Truthy Value
truthyOrFalsy({}); // Truthy Value
//some more truthy valuestruthyOrFalsy(42); // Truthy Value
truthyOrFalsy(new Date()); // Truthy Value
truthyOrFalsy(“Welcome”); // Truthy Value

I hope this article helped you learn about truthy and falsy values in javascript. Please share your experience in using these in your codebase which may help everyone get some more clarity of the concept.

JavaScript — Null vs. Undefined

What is null?

There are two features of null you should understand:

  • null is an empty or non-existent value.
  • null must be assigned.

Here’s an example. We assign the value of null to a:

let a = null;console.log(a);
// null

What is undefined?

Undefined most typically means a variable has been declared, but not defined. For example:

let b;console.log(b);
// undefined

You can also explicitly set a variable to equal undefined:

let c = undefined;console.log(c);
// undefined

Finally, when looking up non-existent properties in an object, you will receive undefined:

var d = {};console.log(d.fake);
// undefined

JavaScript — Double Equals vs. Triple Equals

What is == in JavaScript?

Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison.

So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero. A string with no numeric value is converts to NaN (Not a Number), which returns false.

What is === in JavaScript?

=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.

0==false   // true, because false is equivalent of 0
0===false // false, because both operands are of different type
8=="8" // true, auto type coercion, string converted into number
9==="9" // false, since both operands are not of same type

JavaScript — Use .map(), .find(), and .filter()

map() method

This method is used to apply a function on every element in an array and returns a new array of
same size as the input array.

let newArray = oldArray.map((currentValue, index, array) {
// Returns element to new Array
});

filter() method

This method returns a new array containing the elements that passes a certain test performed
on an original array.

let newArray = oldArray.filter((currentValue, index, array) {
// Returns element to new Array
});

Using Array.find()

The Array.find() method takes a callback function as parameter and executes that function once for each element present in the array, until it finds one where the function returns a true value.

If the element is found it returns the value of the element, otherwise undefined is returned.

var Post = [ 
{ id: 1, title: 'Apple', description: 'Description of post 1' },
{ id: 2, title: 'Orange', description: 'Description of post 2' },
{ id: 3, title: 'Guava', description: 'Description of post 3' },
{ id: 4, title: 'Banana', description: 'Description of post 4' }
];
var found = Post.find(function(post, index) {
if(post.title == 'Guava')
return true;
});

Scope, block scope access outer scope variable

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.

var fruit = 'apple'
console.log(fruit);
function getFruit(){
console.log(fruit);
}
getFruit();

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.

function local() {

let count = 0;
console.log(count);
}


local();

--

--