Some Interesting Javascript programming concepts that every programmer must know.

Kamruzzaman Kamran
5 min readNov 5, 2020
javascript

Do you know that Javascript is a scripting programming language that conforms to the ECMAScript specification? Did you know that JavaScript is both an object-oriented as well as a functional programming language? If you didn’t then you must know that by searching on Google. I’m not here to talk about that today. In this article, I will move over some interesting yet important JS programming concepts.

  • Truthy & Falsy expression: Like any other programming language, javascript has different data types and expressions like Boolean, string, numbers, etc. As you might know, the boolean data type contains two different values which are TRUE and FALSE. But, unlike other programming languages, the JS boolean type is not limited to the True and false, rather it holds two different expressions called Truthy and Falsy. So What are that truth and falsy expression? 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
  • Filter & Find: As a programmer, you must have dealt with the array. In javascript, An array is nothing but a variable, a special kind of variable, which can hold more than one value of more than one type. Obviously, to get or set a value of an array, we need to run this array through a loop. JS has made this thing much easier and simpler by introducing these functions called map(), find(), filter(). They are used to get any value from a Javascript array. the map() is very simple, it’s just an alternative of the “for loop”.

The confusion often goes with the find() and filter() function. They both serve almost the same purpose which is to find values from an array by comparing the condition. The main difference is that the find() function always returns a single value, the first value that matches the condition, whereas the filter() returns all the values of the array that matches the condition. So whenever you need to find a single specific value, you go with the find(), and to get every single value with the condition you use filter(). Here is a simple example below:

const numbers = [4, 6, 8, 10, 12 ];const biggest = numbers.find( (num) => num > 6);
console.log(biggest);//Returns a value 8 //
const bigger = numbers.filter( (num) => num > 6);
console.log(biggest);//Returns an array of value [8, 6, 10,12]//

Now, Here is another interesting info that is you can use those three functions map(), find(), or filter() to get value from any javascript object. Curious ?? I will write about that in another article. In the meantime, you can learn about the javascript object from this MDN documentation.

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

--

--