06 - JavaScript - Operators

06 - JavaScript - Operators

JavaScript operators are symbols or keywords used to perform various operations on one or more values, producing new values. There are several types of operators in JavaScript. Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, and modulus. Comparison operators test if two values are equal or not, while logical operators are used to test if a condition is true or false. Assignment operators store values in variables. Other operators include bitwise, string, and conditional operators, which enable more complex operations. JavaScript operators are useful for performing a range of operations on values, from simple arithmetic to complex logical operations. Whether you need to manipulate binary data, strings or create conditional statements, there is an operator for the job. Overall, JavaScript operators are crucial for web development and programming, making it an essential tool for developers.

Types of JS Operators

Types of JS Operators The different types of JS operators are as follows -

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • String Operators

  • Logical Operators

  • Bitwise Operators

  • Ternary Operators

  • Type Operators

Ternary Operators

In JavaScript, the ternary operator is a shorthand way of writing an if-else statement. It consists of three operands: a condition, a statement to execute if the condition is true, and a statement to execute if the condition is false. The syntax is as follows -

condition ? expression1 : expression2;

If the condition is true, the ternary operator returns expression1, and if the condition is false, it returns expression2.

For example, consider the following code -

let num = 10;
let message = num > 5 ? "Number is greater than 5" : "Number is less than or equal to 5";
console.log(message); // Output: Number is greater than 5

In the above code, the ternary operator checks if the value of num is greater than 5. If the condition is true, the expression "Number is greater than 5" is returned, and if it is false, the expression "Number is less than or equal to 5" is returned.

Ternary operators are useful for writing concise and readable code, especially when you need to assign a value based on a single condition.

Type Operators

In JavaScript, several operators allow you to determine the type of a value or variable. These operators include typeof, instanceof, and Object.prototype.toString.

  1. typeof operator: The typeof operator is used to determine the data type of a value or variable. The syntax is as follows -

     typeof value
    

    For example -

     typeof 42 // Output: "number"
     typeof "hello" // Output: "string"
     typeof true // Output: "boolean"
     typeof null // Output: "object" (note: this is a known bug in JavaScript)
     typeof undefined // Output: "undefined"
    
  2. instanceof operator: The instanceof operator is used to determine if an object is an instance of a specific constructor. The syntax is as follows -

     object instanceof constructor
    

    For example -

     let arr = [1, 2, 3];
     arr instanceof Array // Output: true
     arr instanceof Object // Output: true
    
  3. Object.prototype.toString method: The toString method of the Object.prototype object is used to get the string representation of an object. The syntax is as follows -

     Object.prototype.toString.call(value)
    

    For example -

     Object.prototype.toString.call(42) // Output: "[object Number]"
     Object.prototype.toString.call("hello") // Output: "[object String]"
     Object.prototype.toString.call(true) // Output: "[object Boolean]"
     Object.prototype.toString.call(null) // Output: "[object Null]"
     Object.prototype.toString.call(undefined) // Output: "[object Undefined]"
    

Summarizing Up

JavaScript ternary operator is a shorthand way of writing an if-else statement, which returns a value based on a condition. It has three operands: a condition, an expression to evaluate if the condition is true, and another expression to evaluate if the condition is false. It is used to write concise and readable code when you need to assign a value based on a single condition.

JavaScript type operators are used to determine the type of a value or variable. These operators include typeof, instanceof, and Object.prototype.toString, and are used to perform type checking or type conversion in your code. They are useful for checking the data type of a value or object and performing the appropriate action based on the type.

Did you find this article valuable?

Support manas krishna jaiswal by becoming a sponsor. Any amount is appreciated!