How to

Mastering Unary, Binary, and Ternary Operators in JavaScript

Operators are symbols that let you perform various operations on data. You’ll be familiar with them from basic math, as characters like the + sign, but they work slightly differently in programming.


JavaScript uses many different operators, each with a specific purpose. One way to classify them is by the number of operands they work with: unary, binary, and ternary operators.


Unary Operators in JavaScript

Unary operators are the simplest of the three categories of JavaScript operators. They operate on a single operand, which is a variable or a value. You can use unary operators to increment or decrement a variable, change the sign of a number, or perform logical negation.

Operator

Use

+

Converts to a number

++

Increments value by 1

Converts to a number and negates

Decreases value by 1

!

Inverts a boolean value

Examples of Unary Operators

  1. Increment (++) and decrement (–) operators: Use these operators to increase or decrease the value of a variable by one.
     let x = 5;

    x++;

    x--;

  2. Logical negation (!) operator: Use this operator to reverse the logical value of a boolean expression.
     let isTrue = true;

    let notTrue = !isTrue;

  3. Unary minus (-) operator: This operator changes the sign of a numerical value.
     let y = 10;

    let negY = -y;

Binary Operators in JavaScript

Binary operators take two operands, which can be variables, values, or expressions, and they perform operations on them. You can use binary operators for arithmetic, logical, and comparison operations.

Operator

Use

+

Adds two operands to get the sum

Subtracts the second operand from the first to get the difference

*

Multiplies the two operands

==

Checks the two operands for equality and produces a boolean

Examples of Binary Operators

  1. Addition (+) operator: Adds two numerical values together.
     let sum = 3 + 4;  
  2. Multiplication (*) operator: Multiplies two numerical values.
     let product = 5 * 6;  
  3. Equality (==) operator: Compares two values for equality.
     let isEqual = (x == y);

The Ternary Operator

There is a single ternary operator which you can use to produce more concise code.

Operator

Use

? :

Acts as a shorthand for certain if…else statements

The ternary operator is a conditional that takes three operands: a condition, a value if the condition is true, and another value if the condition is false.

A flowchart showing how the ternary operator works

You should write it like this:

 result = condition ? trueValue : falseValue; 

In this syntax:

  • “condition” is the boolean expression to evaluate.
  • “trueValue” is the value to use if the result of the condition is true.
  • “falseValue” is the value to use if the result of the condition is false.

Here’s an example of how you can use the ternary operator:

 let age = 20;

let status = age >= 18 ? "Adult" : "Minor";

The Power of Unary, Binary, and Ternary Operators

Unary, binary, and ternary operators are crucial for programming. They let you perform a variety of actions on data, clearly and concisely. Whether you’re changing variables, doing math, or making complex decisions, these operators are vital.

Leave a Reply

Your email address will not be published. Required fields are marked *