Some JavaScript programming concepts that every js developer needs to know.

Saniyad Chowdhury
4 min readNov 6, 2020

Do you know that Javascript is a programming language for scripting that complies with the ECMAScript specification? Did you know that JavaScript is both a functional programming language and an object-oriented one? If you didn’t then, by searching on Google, you need to know that. I’m not here today to speak about that. I will go over some interesting yet significant JS programming concepts in this article.

  • Truthy & Falsy expression: Javascript has distinct data types and expressions such as Boolean, string, numbers, etc., like any other programming language. As you might know, there are two different values in the Boolean data form, which are TRUE and FALSE. But the JS Boolean type, unlike other programming languages, is not limited to True and False, but rather has two distinct expressions called Truth and False. So what is that expression of truth and falsity? Well, to understand the terms we must implement any conditional logic like an if-else statement.
let x=1;
if(x){
console.log("You are truthy");
}else{
console.log("You are Falsy");
}

if you see this result on the console, that will be You are truthy. But what if

let x=0;
if(x){
console.log("You are truthy");
}else{
console.log("You are Falsy");
}

in that case, the result will be You are Falsy. Ok That was simple. Everyone knows that 0 is boolean false and 1 is true. So what’s a great deal about it ??

Let's see another example:

let x;
if(x){
console.log("You are truthy");
}else{
console.log("You are Falsy");
}

Guess the result now. If you see this on the console, you will find the result is You are Falsy. Hmm…That's interesting. So why does that happen? In javascript, any variable whose value is not defined is known as an undefined value, and an undefined value has a negative like impression in JS. that is why it’s caught as Falsy value. There are a few more Falsy expression in javascript. They are:

false, 0, -0, 0n, “”, null, undefined, and NaN.

Anything other than those is truthy value in Javascript. Now, let's talk about some of the Falsy keywords

  • Null & undefined value: S0, as I mentioned earlier that any variable whose value is not defined is known as an undefined value. Then what is A NULL value? well, the answer is simple. NULL means empty. If you intentionally want to assign an empty value to a variable, you simply use the NULL keyword. It comes in handy when you try to implement any condition with an empty value. An example is below:

const m = 50;
if (m === null) {
return (“I am empty”);
}else{

return m;

}

  • The ( ==) & ( === ) operator: Did you noticed that, in the previous example, I have used a Comparison operator (===) instead of (==) !! It was not by mistake. There is an important logic in using these operators. Though they both are Comparison operators, there is a key difference between them which is, the == Compares the equality of two operands without considering type, whereas the === Compares the equality of two operands with type. For example:

Let number = “100”;

if (number == 100) }
alert(“Both are equal”);

}
else {
alert(“Both are not equal”);

}

Though the 100 is a string, the result will be “Both are equal”. Because the == doesn't Compare the type of values, only checks the value. But when,

Let number = “100”;

if (number ===100) }
alert(“Both are equal”);

}
else {
alert(“Both are not equal”);

}

The results will be “Both are not equal”. Because the === does Compare the type of values, and when the type and value both match, it gives the compression result is TRUE. Hope that clears the confusion about the two operators.

  • The “NaN”: The NaN is a property of the global object. In other words, it is a variable in global scope. Get’s complicated ?? Let’s make it simpler.

The NaN (Not a NUMBER) is a special type of keyword that use to check if a value is a number or not. It not only checks whether the value is empty “null” or “undefined”, but also checks if the value is a NUMBER or not. In javascript, The isNaN() function often used to check whether a value is “NaN” or not.

Interestingly is, since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself. An Example:

var a = NaN;
a !== a; // true

That’s it for this article. Hope you like those simple information about Javascript.

--

--