Tag: JavaScript

Falsy vs. Nullish Values in JavaScript

Falsy vs. Nullish Values in JavaScript

This is a quick summary on Falsy and Nullish values in JavaScript. JavaScript has 8 falsy values, and 2 nullish values. The two nullish values are also falsy, but the rest of the falsy values are not nullish.

Falsy Values

A falsy value is something that is considered as false when used in a boolean context. JavaScript uses Type Coercion to convert non-boolean values in boolean contexts (such as a conditional, or a loop) into boolean form. There are 8 falsy values:

ValueDescription
falseThe keyword false
0Number 0
-0Negative number 0
0n0 in BigInt
‘’, “”Empty string
nullPrimitive null – no value
undefinedPrimitive undefined
NaNNot a Number
Table 1: Falsy values in JavaScript

Nullish Values

The two primitives null and undefined are known as nullish. Put simply, it means that they don’t have any value. They get converted to false via Type Coercion in a boolean context.

Venn diagram of Falsy and Nullish values in JavaScript.
Figure 1: Venn diagram of Falsy and Nullish values in JavaScript.

By above definitions, we see that nullish values are also falsy.

Materialize CSS Autocomplete with AJAX

Materialize CSS Autocomplete with AJAX

At the time of writing this article, Materialize CSS framework’s Autocomplete widget didn’t work properly with AJAX calls. So I gave it a go myself.

Ideally, the autocomplete widget should have the following features.

  • AJAX calls must be made only after a certain minimum number of characters are entered.
  • Requests must not be sent until the user stops typing into the box. This means setting a reasonable timeout to check end of typing.
  • If a request is already on its way when the user enters more characters, the existing request must be cancelled before sending a new one.
  • Clicking a result or outside the results list must close the list.
  • Certain keys should not trigger AJAX calls.
  • The results must be scrollable with arrow keys and selectable with Enter.

The code looks something like this. A few important things are explained below it.

Read More Read More