Sumita Bhowmick
4 min readNov 5, 2020

--

Some common topics in JavaScript

  1. Truthy & Falsy values in JavaScript: In JavaScript, truthy are expressions which evaluates to boolean true value and falsy evaluates to boolean false value.

then output will come “condition is true”. In JavaScript all numbers except 0 are true. If we consider a string then we can see

Then output will show condition is true. If we put an empty string then output will show “condition is false”.

Here I list which values are truthy:

The following values are always falsy:

2. Null vs Undefined in JavaScript: In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value. You can assign it to a variable.

Here’s an example. We assign the value of null to a:

In the above example, null is assigned to a variable. It means we have defined a variable but have not assigned any value yet, so value is absence.

Undefined most typically means a variable has been declared, but not defined. For example:

In the above example, we have not assigned any value to a variable named ‘b’. A variable ‘b’ lacks a value. So it is undefined.

You can also explicitly set a variable to equal undefined:

Finally, when looking up non-existent properties in an object, you will receive undefined:

3.Double equal vs Triple equal in JavaScript: When using triple equals === in JavaScript, we are testing for strict equality. This means both the type and the value we are comparing have to be the same.

Double equal is known as loose, abstract or type converting equality operator and it automatically converts one type to another if two variable are not the same type. Double equals also performs type coercion.

Type coercion means that two values are compared only after attempting to convert them into a common type.

An example will illustrate this.

55 does not strictly equal '55' because they have different types. However, if we were to test these values with loose equality.

You can see we get true. That because of type coercion. JavaScript will actually try to convert our values into a like type. In this case, it succeeds. The string value of '55' can easily be converted into the number value of 55. Since 55 equals 55, we get our answer of true.

Let’s look one more example:

This is obviously false. However, if we run the same equation with loose equality.

The output is true. It has to do with falsy values in JavaScript. In JavaScript we know that 0 counts as a falsy value.

4.Map(): We can explain it by an example. We have an array containing different object. Each object has an different id and a name. We can get different array containing different id and name. Let’s explain it elaborately

5.Filter(): The filter() array method creates a new array with elements that fall under a given criteria from an existing array:

The example above takes the numbers array and returns a new filtered array with only those values that are greater than seven.

6.Find(): find() is the inbuilt function that is used to get a value of the first item in the array that meets the provided condition. Let’s look at the below example:

Here, both the values 10 and 9 are less than 12, but still, we got the 10 because 10 value of an item is first inside an array. So only 10 will return and not 9.

7.Scope in JavaScript: JavaScript has function scope: Each function creates a new scope. Scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function.

There are two types of scope in JavaScript:

  1. Global scope
  2. Local scope

Variables defined inside a function are in local scope while variables defined outside of a function are in the global scope. Each function when invoked creates a new scope.

8.bind() method in JavaScript: The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Let’s we talk about an example:

Let’s add a greeting function:

We can use the bind method on the greeting function to bind the this keyword to john and jane objects. For example:

Here greeting.bind(john) creates a new function with this set to john object, which we then assign to greetingJohn variable. Similarly for greetingJane.

9.call()method: The call method sets the this inside the function and immediately executes that function.

The difference between call() and bind() is that the call() sets the this keyword and executes the function immediately and it does not create a new copy of the function, while the bind() creates a copy of that function and sets the this keyword.

Above example is similar to the bind() example except that call() does not create a new function. We are directly setting the this keyword using call().

10.apply() method: The apply() method is similar to call(). The difference is that the apply() method accepts an array of arguments instead of comma separated values.

The bind method creates a copy of the function and sets the this keyword, while the call and apply methods sets the this keyword and calls the function immediately.

--

--