What is an Operator in JavaScript

 

What is an Operator?

In programming, an operator is a special symbol or keyword that performs an operation on one or more operands (values or variables). 

Operators are used to manipulate data, perform calculations, compare values, and more. 

Think of them as the tools that help you work with data in your code.

JavaScript Operators

JavaScript, like many programming languages, has various types of operators. Here are the most common categories:

  1. Arithmetic Operators: Perform mathematical calculations.
  2. Assignment Operators: Assign values to variables.
  3. Comparison Operators: Compare two values and return a boolean (true or false).
  4. Logical Operators: Combine multiple conditions.
  5. String Operators: Concatenate (combine) strings.
  6. Unary Operators: Operate on a single operand.
  7. Ternary Operator: A shorthand for an if-else statement.

10 Examples of JavaScript Operators

  1. Addition (+)


    let a = 5; let b = 10; let sum = a + b; // sum is 15
  2. Subtraction (-)


    let difference = b - a; // difference is 5
  3. Multiplication (*)


    let product = a * b; // product is 50
  4. Division (/)


    let quotient = b / a; // quotient is 2
  5. Modulus (%): Returns the remainder of a division.


    let remainder = b % a; // remainder is 0
  6. Assignment (=)


    let x = 20; // x is assigned the value 20
  7. Equality (==): Compares two values for equality, after converting their types if necessary.


    let isEqual = (a == '5'); // isEqual is true
  8. Strict Equality (===): Compares two values for equality without converting their types.

    let isStrictEqual = (a === '5'); // isStrictEqual is false
  9. Logical AND (&&): Returns true if both conditions are true.


    let result = (a > 0 && b > 0); // result is true
  10. Ternary Operator (condition ? expr1 : expr2): A shorthand for if-else.


    let max = (a > b) ? a : b; // max is 10

These examples give a basic overview of how operators work in JavaScript. 

Operators are essential for performing tasks in your code, whether it's doing math, making decisions, or combining conditions.

Previous Post Next Post

Welcome, New Friend!

We're excited to have you here for the first time!

Enjoy your colorful journey with us!

Welcome Back!

Great to see you Again

If you like the content share to help someone

Thanks

Contact Form