person typing
JavaScript Various Types Of Operations

JavaScript Operations

JavaScript operators assign values, compare values, execute arithmetic operations, and so on. It's likely that you are already familiar with many of these like addition +, subtraction -, multiplication *, and division /.

JavaScript Expressions

A JavaScript expression contains both operators and operands. In general, it can be regarded as any valid unit of code that corresponds to a value.

  1. operand - what operators are applied to. For instance in the expression 8 * 3 there are two operands. The left operand is the 8 and the right operand is the 3. Operands can also be referred to as arguments.

  2. operator - the verb of the expression that denotes what we're doing to the operands. JavaScript has the following types of operators:

assignment operators

comparison operators

arithmetic operators

bitwise operators

logical operators

string operators

conditional (or ternary) operators

comma operator

unary operator

relational operators
  1. unary expression - consists of a single operand, one before or one after the operator:

operator operand: ++x; or: operand operator x++;

  1. binary expression - requires two operands: operand1 operator operand2
js
3 + 4
6 * y
  1. ternary expression - an only JavaScript operator that creates a ternary expression is a conditional operator with the syntax: condition? val1 and val2. Assuming condition is true, the operation has the result of val1, otherwise, it has the result of val2.
js
age >= 21 ? 'alcoholic' : 'non-alcoholic'

Assignment Operators

Using the assignment operator, one assigns a numerical value to its left operand depending on that of its right operand. For simple assignments, you can use =

js
x = y

For complex assignments, you can use compound assignment operators like the following examples:

js
x += y
x -= y

Which is equivalent to:

js
x = x + y
x = x - y

Destructuring

In complex assignment problems, you can use destructuring to extract data from arrays or objects using a syntax that matches the structure of array or object data.

js
let foo = ['one', 'two', 'three']
// without destructuring
let one = foo[0]
let two = foo[1]
let three = foo[2]
// with destructuring
let [one, two, three] = foo

Comparison Operators

In the comparison operator, operands are examined and a logical result is generated (true or false) based on the result of the comparison.

random stuff

Look at the more details: MDN

Arithmetic Operators

The arithmetic operators use numerical values (such as words or variables) during their operands and always assign one number.

random stuff

Look at the more details: MDN

Bitwise Operators

Bitwise operators handle operands in the context of a set of 32 bits (ones and zeros), rather than decimal, octal, or hexadecimal numbers. For instance, decimal 10 has a binary number of 1010. In JavaScript, Bitwise operators are assigned to such binary values, however, they return standard JavaScript numerical values.

random stuff

Look at the more details: MDN

Logical Operators

There are several logic operators that are commonly used with boolean (logic) values; when they are returning a boolean value. Although the && and || operators always return the value this is the specified operands, therefore when these operators interact with non-Boolean values, they may return a non-Boolean result.

random stuff

Look at the more details: MDN

String Operators

The + operator (in conjunction with the += compound operator) is also effectively used as a concatenation operator when dealing with strings.

js
console.log('alpha' + 'bet') // logs the string 'alphabet' to the console

Other Unary Operators

delete

The delete operator removes an element, an object's attribute, or an element at a specific index in an array

js
delete objectName
delete objectName.property
delete objectName[index]

typeof

The typeof operator returns a string specifying the type of the undetected operand with the syntax typeof operand. an operand is a keyword, string, or variable for which type is to be returned

js
let shape = 'round'
let size = 1
let foo = ['Apple', 'Mango', 'Orange']
typeof shape // returns "string"
typeof size // returns "number"
typeof foo // returns "object"

void

The void operator instructs an expression it should be evaluated without returning a value with the syntax void evaluate, expression is a JavaScript expression to be evaluated.

js
<a href="javascript:void(0)">Click here to do nothing</a>

Relational Operators

in

The in operator indicates the given property in the specified data.

js
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple']
3 in trees // returns true
6 in trees // returns false
'length' in trees // returns true (length is an Array property)
'PI' in Math // returns true
let mycar = { make: 'Honda', model: 'Accord', year: 1998 }
'make' in mycar // returns true

instanceof

The instanceof operator returns true when the specified object of the specified object category.

js
let theDay = new Date(2018, 1, 1)
theDay instanceof Date // returns true

Operator Precedence

The appearance of operators indicates the order they are executed when computing an expression. You can modify operator ordering by using brackets.

The following table describes the precedence of operators, from highest to lowest:

random stuff

Look at the more details: MDN

all posts

©2021-2021 DevsrcBlog. All rights reserved.