JavaScript Operator Precedence

Operator precedence describes the order in which operations are performed in an arithmetic expression.

Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).

As in traditional mathematics, multiplication is done first:

let x = 100 + 50 * 3;

When using parentheses, operations inside the parentheses are computed first :

let x = (100 + 50) * 3;

Operations with the same precedence (like * and /) are computed from left to right : Continue reading JavaScript Operator Precedence

JavaScript Regular Expressions

A regular expression is a sequence of characters that forms a search pattern.

The search pattern can be used for text search and text replace operations.

What Is a Regular Expression?

A regular expression is a sequence of characters that forms a search pattern.

When you search for data in a text, you can use this search pattern to describe what you are searching for.

A regular expression can be a single character, or a more complicated pattern.

Regular expressions can be used to perform all types of text search and text replace operations. Continue reading JavaScript Regular Expressions

JavaScript Bitwise Operations

JavaScript Bitwise Operators

Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>> Zero fill right shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off

Continue reading JavaScript Bitwise Operations

JavaScript Destructuring

Destructuring Assignment Syntax

The destructuring assignment syntax unpack object properties into variables:

let {firstName, lastName} = person;

It can also unpack arrays and any other iterables:

let [firstName, lastName] = person;

Object Destructuring

Example

// Create an Object
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50
};

// Destructuring
let {firstName, lastName} = person;

The order of the properties does not matter : Continue reading JavaScript Destructuring

JavaScript Type Conversion

  • Converting Strings to Numbers
  • Converting Numbers to Strings
  • Converting Dates to Numbers
  • Converting Numbers to Dates
  • Converting Booleans to Numbers
  • Converting Numbers to Booleans

JavaScript Type Conversion

JavaScript variables can be converted to a new variable and another data type:

  • By the use of a JavaScript function
  • Automatically by JavaScript itself

Continue reading JavaScript Type Conversion

JavaScript typeof

The typeof Operator

The typeof operator returns the data type of a JavaScript variable.

Primitive Data Types

In JavaScript, a primitive value is a single value with no properties or methods.

JavaScript has 7 primitive data types:

  • string
  • number
  • boolean
  • bigint
  • symbol
  • null
  • undefined

The typeof operator returns the type of a variable or an expression.

Examples

typeof "John"         // Returns string
typeof ("John"+"Doe") // Returns string
typeof 3.14           // Returns number
typeof 33             // Returns number
typeof (33 + 66)      // Returns number
typeof true           // Returns boolean
typeof false          // Returns boolean
typeof 1234n          // Returns bigint
typeof Symbol()       // Returns symbol
typeof x              // Returns undefined

typeof null           // Returns object

Note:

In JavaScript, null is a primitive value. However, typeof returns “object”.

This is a well-known bug in JavaScript and has historical reasons.

Continue reading JavaScript typeof

JavaScript Map Methods

The new Map() Method

You can create a map by passing an array to the new Map() constructor:

Example

// Create a Map
const fruits = new Map([
  ["apples", 500],
  ["bananas", 300],
  ["oranges", 200]
]);

Map.get()

You get the value of a key in a map with the get() method Continue reading JavaScript Map Methods

JavaScript Maps

A Map holds key-value pairs where the keys can be any datatype.

A Map remembers the original insertion order of the keys.

How to Create a Map

You can create a JavaScript Map by:

  • Passing an Array to new Map()
  • Create a Map and use Map.set()

The new Map() Method

You can create a Map by passing an Array to the new Map() constructor : Continue reading JavaScript Maps

JavaScript Set Methods

The new Set() Method

Pass an array to the new Set() constructor:

Example

// Create a Set
const letters = new Set(["a","b","c"]);

The add() Method

Example

letters.add("d");
letters.add("e");

If you add equal elements, only the first will be saved : Continue reading JavaScript Set Methods

JavaScript Sets

A JavaScript Set is a collection of unique values.

Each value can only occur once in a Set.

The values can be of any type, primitive values or objects.

How to Create a Set

You can create a JavaScript Set by:

  • Passing an array to new Set()
  • Create an empty set and use add() to add values

Continue reading JavaScript Sets