One of the most valuable topics in JavaScript is Truthy and Falsy values. Let’s have a look there.

md rana
4 min readMay 9, 2021

TRUTHY and FALSY

  • In javascript any variable with an assigned value without zero is true. Like- 1, -1, 3, etc.
  • When the variable is declared without value, it is undefined. Undefined is always false.
  • Variable with default value null is always false.
  • Variable with value NaN is false.
  • Empty array and empty object are true.
  • If zero is used for if, else if condition then it is true.
  • An empty string is false.

Another important topic in JavaScript is Undefined and Null. So we must know about that.

UNDEFINED:

  • What is not defined is called undefined. Like, let age;
  • When a function explicitly does not return anything. Example:

function subtraction(x, y){

console.log(x-y);

}

const result = subtraction(20, 5);

console.log(result); // here result is undefined.

  • return without any value or variable id undefined. Example:

function………..(){

// code ….

rerurn;

}

  • If any parameters value is not passed then it is undefined.Example:

function subtraction(x, y){

return (x-y);

}

const result = subtraction(20); // y is undefiend.

  • Accessing any object property which is missing is undefined. Example:

const person = {

name: ‘Hridi’,

age:23

}

condole.log(person.job); //undefiend as job is not a property.

  • Any variable declared with a value undefined is undefined.Such as- let name = undefined;
  • let ages = [5, 7 ,9]; console.log(ages[3]); //there is no value in index 3

NULL:

  • Defines the value is not there.

Such as- let name = “Hridi”;

name = null;

  • We have to set explicitly null for getting null.

We can make undefined in many ways but we declare explicitly any variable as null, this is the difference between them.

Most often we use double equal and triple equal in the conditions but we do not know the usage of them. It is very necessary to know about these.

DOUBLE vs TRIPPLE EQUAL

Double equal only checks value not type. Exmaple:

(2 == “2”); // true

Here bother the value is 2 and that’s why it is true. But the first one is number type and the second is a string.

Triple equal checkes both value and type. Example:

(2 === “2”); // false

Here both the value is the same which is 2. But the first one is number type and the second one is a string. That’s why it returns false.

Check some examples:

const num1 = 1;

const num2 = true;

This is true for double equal but false for triple equal. Again,

const num1 = 0;

const num2 = false;

This is true for double equally but false for triple equal.

Do you know about JavaScript scope, hoisting, and closure? No worries. I am going to make it a little bit easier for you.

SCOPE

When you use curly braces then it calls scope of that part. You can declare a variable inside that and that is called the scope of that variable. Like-

{

let result = 10;

}

This is called the block scope of that variable. Any variable outside functions and any scopes are called Global variables.

Another thing is called Hoisting. It is an interesting thing. Let’s see what it says.

HOISTING

If we declare any variable with the keyword “var” then we can use it outside that block. It is called hoisting. But we cannot use it before declaration as JavaScript hoist only variable, not value. But we can not hoist with let and const declaration. It then works under the parent scope. Like-

function add(a, b){

// console.log(resul) is undefined.

if(a>2){

var result = a+b;

}

console.log(result);

}

console.log(result);

CLOSURE

When we have a function inside another function then the inner function makes its scope for outside function. Then the scope of the inner function is called closure. Example:

function user(){

let name;

return userName(){

name = “Timittra”;

}

}

As we are talking about scope, so why not learn a little more about the window, global variable, and global scope.

WINDOW

This is the place where javascript is running. It includes all the elements of the website. This is the execution of javascript. Like-

document === window.document;

//true

console === window.console;

// true

But we can not access id, class directly by window. We need to call those by the document. Like-

document.getElementById(‘id_name’);

The window is called global scope as it is accessed by all. Likewise, any function that is under another function is called the local function. And the function that is outside another function is called the global function. If we call a local variable or function from the window then we will get an error. In another, we can make some variables global. Such as-

function add(a, b){

result = a+b;

}

Here the result is by default global We make it an implicitly global variable.

Again: window.result = a + b; // global

But if we call this before the function declares then we will get an error. If we add any variable using a window that is not on the site before, will be included after. Like- window.salary = 9000;

We use JavaScript but we don’t know “this” keyword, which can not be possible.

THIS

Any method and function are running in which context, that context is called this. Like-

function add(a, b){

console.log(this);

// this means here window as it is not an object, direct function.

return (a+b);

}

In this case, as an object is connected by a dot and calling the same add function, so this time “this” keyword returns myObject, not window. If code performs event handler at any DOM element then the targeted value is known as this.

Almost we come to the last point for today. So make the finishing more enjoyable with a code for counting words in the string. Example:

WORD COUNT

var sentence = “I am a programmer and I also love web development.”;

var count = 0;

for(var i=0; i<sentence.length; i++){

var letters = sentence[i];

if(letters == “ “ && sentence[i-1] != “ “ ){

count++;

}

}

count++;

consoole.log(“Total words: “, count);

// Total words: 10

So that’s all for today.

--

--