Use Unary Operator to Convert Non Numbers to Numbers

The unary plus (+) operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn’t already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null....

December 28, 2022 · 1 min · Daman Arora

Always Follow AAA Pattern When Writing Tests

Following is a very simple unit test which verifies if the add() function works correctly. While the test works, but it does not follow the AAA(Arrange, Act, Assert) pattern. import {it, expect} from 'vitest'; import { add } from './math'; it('should summarize all number values in an array', ()=>{ const result = add([1,2,3]) expect(result).toBe(6) }); Question: What is AAA (Arrange, Act, Assert) pattern in testing? AAA pattern allows you to write unit tests which are more readable and maintainable in long term....

December 27, 2022 · 1 min · Daman Arora

Modules and Classes in ES6

Modules ES6 has added Modules to JavaScript. This means, that you may split up your code over multiple files, which of course is a good practice. This is common in ES6 already, however you always require a module loader for that. To split up your code, you basically export variables, functions, objects, … in one file and import it in another: // export.js export let myExportedVar = 42; // import.js import { myExportedVar } from '....

December 26, 2022 · 1 min · Daman Arora

Two Important Rules of ES6 Modules

There are two important Rules, which you need to understand if you’re working with ES6 Modules: Modules are always in Strict Mode (no need to define "use strict"). Modules don’t have a shared, global Scope. Instead each Module has its own Scope.

December 26, 2022 · 1 min · Daman Arora

Destructuring an Array or an Object in ES6

Destructuring is a cool new feature which allows you to easily extract values from an object or an array. It is important to note, that destructuring for arrays is position based, whereas in case of the objects it is based on the names of the keys. Array: let numbers = [1, 2, 3, 4, 5]; let [a, b] = numbers;// a => 1, b => 2 Object: let person = { name: 'Max', age: 27 }; let {name, age} = person; // Notice the {} instead of [] More information may be found here....

December 25, 2022 · 1 min · Daman Arora