Basic validations every nodejs developer should know

Sunday, September 6, 2020 • 7 minutes to read

NodejsArticle2020good-practicetips

Validations are the key things to prevent the application's unexpected behaviour. This is a list of basic validations every nodejs developer should know about.
I will keep updating this article with new validation and best approaches.

typeof operator

Javascript provides a typeof operator. This operator returns the type of the variable.

Syntax

1typeof operand;
2typeof operand;

Return Values of the typeof

Type Return
Number “number”
String “string”
Boolean “boolean”
Undefined “undefined”
Null “object”
Function “function”
Object “object”

There are a couple of other like BigInt(returns “bigint”) which introduced in ECMAScript 2020 and Symbol(returns “symbol”) which introduced in the ECMAScript 2015. Learn more

Number check

For all the numbers(integer, floating) and Infinity, it returns number.

 1const num1 = 12;
 2
 3// check if it is a number
 4if (typeof num1 === "number") {
 5  console.log("It is a number");
 6} else {
 7  console.log("It is not a number");
 8}
 9
10// check for Infinity
11const num2 = Infinity;
12
13// check if it is a number
14if (typeof num2 === "number") {
15  console.log("It is a number");
16} else {
17  console.log("It is not a number");
18}
19
20// check for string
21const num3 = "2";
22
23// check if it is a number
24if (typeof num3 === "number") {
25  console.log("It is a number");
26} else {
27  console.log("It is not a number");
28}

Output

1It is a number
2It is a number
3It is not a number

Check NaN (not a number)

NaN is a property and its type is number. NaN is never equal to any number including NaN itself.

NaN != NaN

To check NaN, always use isNaN method.

1const str = "js";
2
3// check if it is not a number
4if (isNaN(str)) {
5  console.log("It is not a number");
6} else {
7  console.log("It is a number");
8}

Output

1It is not a number

Check if the number is finite

isFinite method returns a boolean value if a number is finite or not.

 1const num1 = 12;
 2
 3// check if finite
 4if (isFinite(num1)) {
 5  console.log("Number is finite");
 6} else {
 7  console.log("Number is not finite");
 8}
 9
10const num2 = Infinity;
11
12// check if finite
13if (isFinite(num2)) {
14  console.log("Number is finite");
15} else {
16  console.log("Number is not finite");
17}

Output

1Number is finite
2Number is not finite

String Check

1// check for string
2const str = "2";
3
4// check if it is a string
5if (typeof str === "string") {
6  console.log("It is a string");
7} else {
8  console.log("It is not a string");
9}

Output

1It is a string

Boolean Check

 1// check for boolean
 2const bool = true;
 3
 4// check if it is a boolean
 5if (typeof bool === "boolean") {
 6  console.log("It is a boolean");
 7} else {
 8  console.log("It is not a boolean");
 9}
10
11// check for boolean
12const str = "str";
13
14// check if it is a boolean
15if (typeof str === "boolean") {
16  console.log("It is a boolean");
17} else {
18  console.log("It is not a boolean");
19}

Output

1It is a boolean
2It is not a boolean

Undefined Check

1// check for undefined
2const variable = undefined;
3
4// check if it is undefined
5if (typeof variable === "undefined") {
6  console.log("It is undefined");
7} else {
8  console.log("It is not undefined");
9}

Output

1It is undefined

Null Check

typeof null returns Object.

Check out why it returns Object.

1// check for null
2const variable = null;
3
4// check if it is a null
5if (variable === null) {
6  console.log("It is a null");
7} else {
8  console.log("It is not a null");
9}

Output

1It is a null

Null check using the Object

1function isNull(variable) {
2  return Object.prototype.toString.call(variable) === "[object Null]";
3}

Function check

 1// check for function
 2const variable = function (x, y) {
 3  return x + y;
 4};
 5
 6// check if it is a function
 7if (typeof variable === "function") {
 8  console.log("It is a function");
 9} else {
10  console.log("It is not a function");
11}

Output

1It is a function

Object Check

There are multiple ways to check if the variable is an object or not.

 1// check for Object
 2const variable = {
 3  id: 12,
 4};
 5
 6// check if it is an Object
 7if (typeof variable === "object") {
 8  console.log("It is an Object");
 9} else {
10  console.log("It is not an Object");
11}

Output

1It is an Object

Note: null is also an object

Check the object and not null.

 1// check for Object and not null
 2const variable = {
 3  id: 12,
 4};
 5
 6// check if it is an Object
 7if (typeof variable === "object" && variable !== null) {
 8  console.log("It is an Object and not null");
 9} else {
10  console.log("It is not an Object");
11}

Output

1It is an Object and not null

Check using the instanceof

The instanceof operator returns boolean, it checks if the variable has a prototype property of a constructor exists in the prototype chain of an object.

The instanceof operator also takes care of the null object. It returns false to null.

 1// check for Object
 2const variable = {
 3  id: 12,
 4};
 5
 6// check if it is an Object
 7if (variable instanceof Object) {
 8  console.log("It is an Object");
 9} else {
10  console.log("It is not an Object");
11}
12
13// check for Object
14const variable2 = null;
15
16// check if it is an Object
17if (variable2 instanceof Object) {
18  console.log("It is an Object");
19} else {
20  console.log("It is not an Object");
21}

Output

1It is an Object
2It is not an Object

Array Check

An array is also an Object. Using typeof requires multiple validations to validate it.

Empty array is a true value.

In javascript Array class is a global object that is used in the construction in the arrays.
Array class has many methods and one of them is isArray.

isArray returns true if the variable is an array else false.

 1// check for Array
 2const arr = ["hello", "world"];
 3
 4// check if it is an Array
 5if (Array.isArray(arr)) {
 6  console.log("It is an Array");
 7} else {
 8  console.log("It is not an Array");
 9}
10
11// check for Array
12const arr2 = 12;
13
14// check if it is an Array
15if (Array.isArray(arr2)) {
16  console.log("It is an Array");
17} else {
18  console.log("It is not an Array");
19}

Output

1It is an Array
2It is not an Array

Other alternatives to check the array.

1// conditions
21. arr.constructor === Array
3
42. arr instanceof Array
5
63. Object.prototype.toString.call(arr) === '[object Array]'

Buffer Check

In the nodejs, Buffer class is in the global scope.
Buffer class has an isBuffer method to check if the variable is buffer or not.

Buffer objects are used to represent a fixed-length sequence of bytes. The Buffer class is a subclass of JavaScript's Uint8Array class and extends it with methods that cover additional use cases. Node.js APIs accept plain Uint8Arrays wherever Buffers are supported as well.

1// check for Buffer
2const buf = Buffer.from("Hello World");
3
4// check if it is a Buffer
5if (Buffer.isBuffer(buf)) {
6  console.log("It is a Buffer");
7} else {
8  console.log("It is not a Buffer");
9}

Output

1It is a Buffer

⚠️ Buffer class is not available in the Javascript. Don't try it in client-side application it will throw an error.

Conclusion

I'll keep updating it with frequent validation a javascript or nodejs developer has to do every day.
Thanks for reading and please let me what else can be added in this.


NodejsArticle2020good-practicetips

PreviousFlashback 2020

NextCreate a Serverless Application in Golang With AWS