Operators in Javascript

Operators in Javascript

According to tutorialsteacher, An operator performs some operation on single or multiple operands (data value) and produces a result. Javascript like any other language has a couple of operators. Today, we will dive into Arithmetic and Logical operators.

Arithmetic operators

The Arithmetic operator includes subtraction -, addition +, multiplication *, division /, and Remainder operator. %. They are mostly used with number and string types.

The addition operator can be used to concatenate strings,

let firstName = 'Ned';
let lastName = 'Stark;

let fullName = firstName + lastName;
// Ned Stark

The rest work with number types

console.log(3 + 5);
// expected output: 8
console.log(8 - 8);
// expected output: 0
console.log(3 * 5);
// expected output: 15
console.log(8/2);
// expected output: 4

Logical Operators

The Logical Operators are OR ||, AND assignment &&, Nullish operator ??.

The OR operator (||) has multiple operands and returns the right operand if the left operand is false, otherwise it returns its right operand.

const title = ''
console.log(title || 'comissioner');
// expected output: comissioner

The AND operator (&&) takes multiple boolean operands and will be true if and only if all the operands are true. Else, it will be false.

console.log(true && true && false);
// expected output: false

console.log(true && true && true);
// expected output: true

The nullish coalescing operator ?? returns its right operand if its left operand is null or undefined, and otherwise returns its left operand.

console.log(null ?? 'random stuff');
// expected output: "default string"

console.log( 0 ?? 4);
// expected output: 0

Conclusion

In this article, you were re-introduced to arithmetic and logical operators. This article provides a mental break from all of the technical stuff you've been reading recently.