
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,
1let firstName = 'Ned';2let lastName = 'Stark;34let fullName = firstName + lastName;5// Ned Stark
The rest work with number types
1const title = ''2console.log(title || 'comissioner');3// expected output: comissioner4
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.
1console.log(true && true && false);2// expected output: false34console.log(true && true && true);5// expected output: true6
The nullish coalescing operator ?? returns its right operand if its left operand is null or undefined, and otherwise returns its left operand.
1console.log(null ?? 'random stuff');2// expected output: "default string"34console.log( 0 ?? 4);5// expected output: 06
Conclusion
In this article, you were re-introduced to arithmetic and logical operators.