Every JavaScript Developer Need To Know

Mahabub Sunny
4 min readMay 8, 2021

Today we are discussing Truthy and Falsy Values in JavaScript, Null Vs Undefined, Double Equals, and Triple Equals, Global Scope, Key Features Of JavaScript, What Is DOM

Truthy and Falsy Values in JavaScript

Longtime JavaScript developers often toss around the terms “truthy” and “falsy”, but for those who are newer to JavaScript, these terms can be a bit mystifying.

When we say that a value is “truthy” in JavaScript, we don’t just mean that the value is true. Rather, what we mean is that the value coerces to true when evaluated in a boolean context. Let's look at what that means.

function logTruthiness (val){
if(val){
console.log("Truthy!");
}else{
console.log("Falsy.");
}
}

This function takes the val parameter and evaluates it in a boolean context (the condition of the if statement.) So let's try it out on some values.

// Outputs: "Truthy!"
logTruthiness(true);
// Outputs: "Truthy!"
logTruthiness({});
// Outputs: "Truthy!"
logTruthiness([]);
// Outputs: "Truthy!"
logTruthiness("some string");
// Outputs: "Truthy!"
logTruthiness(new Date());

As you can see, there are a lot of truthy values in JavaScript. And there are many more that could be listed here. On the other side, though, there are only six falsy values. In fact, because the list of falsy values is so short, memorizing that list is the easiest way to tell whether a value is truthy or falsy. Here is the list:

// Outputs: "Falsy."
logTruthiness(false);
// Outputs: "Falsy."
logTruthiness(null);
// Outputs: "Falsy."
logTruthiness(undefined);
// Outputs: "Falsy."
logTruthiness(NaN);
// Outputs: "Falsy."
logTruthiness(0);
// Outputs: "Falsy."
logTruthiness("");

Null Vs Undefined

To start I want to explain what null and undefined have in common since they are very similar. Both null and undefined mean that there is no value. If a variable is set to null or undefined it has no value and if a function returns null or undefined then it is saying it has no value to return. This you most likely already understand.

These values are actually so similar that they are considered equal when comparing with double equals (==).

for example:

console.log(null == undefined)
// true
console.log(null === undefined)
// false

Because of this, when I want to check to see if a variable has a value or not I almost always use double equals comparison since it will return true whether the variable is null or undefined.

Double Equals, and Triple Equals

Unlike many other programming languages, JavaScript has both double equals == and triple equals === comparison operator. At first glance, they seem to do nearly the exact same thing, but there is one major difference between the two that makes triple equals almost always better.

What Makes Them Different

Both double and triple equals will check the equality of two values and return true or false depending on if they are equal or not, but the way they perform that equality check is slightly different. With double equals, JavaScript will convert the types of the two values to be exactly the same before doing an equality check. This means that if you have the following code 1 == '1' then JavaScript will convert the string '1' into a number so that the equality check is comparing two numbers. This would result in the previous code returning true since the number one is equal to the string one after it is converted into a number.

With triple equals, this conversion of types does not occur. That means that if we use the same code as before but with a triple equals comparison we will get false 1 === '1'. This is because the string '1' is not converted into a number and thus when the equality of the number one is compared to the string one it returns false since a string is never equal to a number.

Global Scope

Let’s begin by considering the global scope. Globally declared variables can be accessed and modified anywhere in the code (well almost, but we will come to the exceptions later).

Global variables are declared in the code outside of any function definitions, in the top-level scope.

// Golbal
var
// Local
const

Key Features Of JavaScript

JavaScript is divided into two main features, they are as follows –

General JavaScript Features

JavaScript language consists of several different features. Some of the general JavaScript features are as follows –

1. Validating User’s Input
2. Simple Client-side Calculations
3. Greater Control
4. Platform Independent
5. Handling Dates and Time
6. Generating HTML Content
7. Detecting the User’s Browser and OS

Modern JavaScript Features

If we dive into some more recently added features of JavaScript that make it unique from other programming languages. There are a lot more modern features of JavaScript invented after some general features. Some of them are as follows –

1. Let/Const
2. Arrow Functions
3. Template Literal
4. New Array Functions
5. Default Parameters
6. Property Shorthand

What Is DOM

DOM is an application program interface for HTML, XHTML, XML, and others. It takes the document and basically makes it into a tree structure. Each box on the tree structure is an element in HTML. When it does that, it organizes the document in a way that will make it able to be used in other languages like JavaScript.

Thank you for reading 😊

--

--