DEV Community

Cover image for Recap Javascript Basics
Delwar Ahmed
Delwar Ahmed

Posted on

Recap Javascript Basics

Here I'll discuss 10 things about javascript.

1. Truthy and Falsy values

Truthy values are:

  • Any number except 0 (zero) is truthy.
    const a = 12;

  • Any string that contains some value (at least white space) is truthy.
    const a = 'something';
    or
    const a = ' '; // a white space

  • Empty array is truthy.
    const array = [];

  • Empty object is truthy.
    const obj = {};

  • Empty function is truthy.
    function functionName () {};
    or
    const functionName = () => {};

Falsy values are:

  • Integer 0 (zero) is falsy. const number = 0;
  • Empty string is falsy. const foo = '';
  • undefined is falsy.
  • null is falsy.
  • NaN (Not a Number) is falsy.
  • false keyword is falsy.

2. Javascript double equal sign (==) vs triple equal sign (===)

Double equal sign usages:

Double equal sign Triple equal sign
Double Equals (==) checks for value equality only Triple Equals (===) does not convert type
Does type coercion inherently. Does not perform type coercion.
That means before checking value, it converts the types of the variables to match each other It verifies whether the variable is being compared have both the same value AND the same type.

3. Scope and Block Scope in Javascript

Scope

Scope determines the accessibility of a variable declared in the area of your code. There are three types of scope. These are Global Scope, Local Scope, Function Scope, Block Scope, Lexical Scope.
I'm gonna discuss two of them.

Global Scope

The area outside all the functions
considered the global scope and the variables defined inside the global scope can be accessed and altered in any other scopes.

//global scope
const fruit = 'apple'
console.log(fruit);        //apple

const getFruit = () =>{
    console.log(fruit);    //fruit is accessible here
}

getFruit();                //apple

Enter fullscreen mode Exit fullscreen mode

Block Scope

The concept of block scope is introduced in ES6 together with the new ways to declare variables -- const and let.

const foo = () =>{
    if(true){
        var fruit1 = 'apple';        //exist in function scope
        const fruit2 = 'banana';     //exist in block scope
        let fruit3 = 'strawberry';   //exist in block scope

    }
    console.log(fruit1);
    console.log(fruit2);
    console.log(fruit3);
}

foo();
//result:
//apple
//error: fruit2 is not defined
//error: fruit3 is not defined
Enter fullscreen mode Exit fullscreen mode

Top comments (0)