Basic Operators of Javascript

Let's start by defining what an Operator is. An Operator allows us to transform values or combine multiple values and do all kinds of work with values. And there are many categories of Operators like Mathematical Operators, Comparison Operators, Logical Operators, Assignment OperatorsAnd many more. In this post, let's look at some of these operators.

Mathematical or Arithmetic Operators

We already use the + and - operators, but of course, we can do all Arithmetic Operations. We can also do division, multiplication, and many more.

const x = 10;
const y = 2;
let result;

// Addition Operator
result = x + y;
console.log(result); // 12

// Substration Operator
result = x - y;
console.log(result); // 8

// Multiplication Operator
result = x * y;
console.log(result); // 200

// Division Operator
result = x/y;
console.log(result); // 5

// Modulous or Reminder Operator
result = x % y;
console.log(result); // 0

// Exponential Operator
result = x ** y // x to the power of y
console.log(result); // 100

Plus Operator

plus operator is not only a Mathematical Operator but also can be used with strings. When used with strings, it joins the strings or concatenate strings.

const firstName = 'Akhil';
const lastName = 'Naidu';

console.log(firstName + lastName); // AkhilNaidu

// We can also join space
console.log(firstName + ' ' + lastName) // Akhil Naidu
plus operator also concatenate or joins two strings

typeof

We used this operator extensively in one of our previous posts. This is repeated just for the sake of documenting with the rest of the operators. A typeof operator helps us determine the type of the variable.

const hero = 'Naruto';
console.log(typeof hero) // string

const myAge = 24;
console.log(typeof myAge) // number

const jsIsFun = true;
console.log(typeof jsIsFun) // boolean

let emptyVariable;
console.log(typeof emptyVariable) // undefined
revisiting typeof operator
const thisIsABug = null;
console.log(typeof thisIsABug) // Object

console.log(typeof null) // Object
This is a known bug in javascript and it is intentionally not updated for legacy reasons. The correct output should be undefined

Assignment Operators

The most straightforward assignment operator is just the equal sign =.

// x will be assigned 30
// Plus will be executed before the assignment operator
let x = 10 + 20;
console.log(x) // 30

x += 10; // x = x + 10 => 30 + 10
console.log(x) // 40

x *= 2; // x = x * 2 => 40 * 2
console.log(x) // 80

x++; // x = x + 1 => 80 + 1
console.log(x) // 81

x--; // x = x - 1 => 81 - 1
console.log(x) // 80
This equal sign, =, is actually an operator.

Comparison Operators

Comparison Operators are actually great, we use them to produce boolean values. It would be better to demonstrate them using a code example similar to assignment operators.

const akhilAge = 25;
const narutoAge = 20;

console.log(akhilAge > narutoAge); // true
console.log(akhilAge < narutoAge); // false

// != means not equal
console.log(akhilAge != narutoAge); // true
Result of a comparison operator should be a boolean

These are very useful when we start making decisions with our code based on conditions like above.

Other comparison operators are >, <, >=, <=, !=, !==, ==, ===; introducing some of these comparisons right now is not a good idea, so I kept them for a later post.

Loading comments...
You've successfully subscribed to Leewardslope
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.