JavaScript 10

JavaScript 10

The Real-Deal with Examples

Javascript Operators

JS operator are used to perform particular actions. They can be used to assign values, combine values, perform arithmetic operations and more. JS operators are symbols (or set of symbols) or keywords.

There are different types of operators namely:

  • Assignment Op
  • Arithmetic Op
  • Comparison Op
  • Logical Op

    1.Assignment Opertors

    The assignment operator is used to assign a value to a variable. It uses the equal = symbol.
var x = 3; // assigns number 3 to x
var y = 7; // assigns number 7 to y
var sum = x + y; // assigns the sum of x and y to sum
document.write(sum); // prints sum

2. Arithmetic Operators

The arithmetic operators are used to perform arithmetic operations (like addition, subtraction, multiplication, and division) between values. Here are the symbols used for the arithmetic operators:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /

Addition Example:

var num1 = 3; // assigns number 3 to num1
var num2 = 7; // assigns number 7 to num2
var sum = num1 + num2; // assigns the sum of num1 and num2 to sum
document.write(sum); // prints sum

Subtraction Example:

var num1 = 3; // assigns number 3 to num1
var num2 = 7; // assigns number 7 to num2
var difference = num1 - num2; // assigns the difference of num1 and num2 to difference
document.write(difference); // prints difference

Multiplication Example:

var num1 = 3; // assigns number 3 to num1
var num2 = 7; // assigns number 7 to num2
var product = num1 * num2; // assigns the product of num1 and num2 to product
document.write(product); // prints the product

Division Example:

var num1 = 3; // assigns number 3 to num1
var num2 = 7; // assigns number 7 to num2
var quotient = num1 / num2; // assigns the quotient of num1 and num2 to quotient
document.write(quotient); // prints the quotient

3. Comparison Operators

Comparison operators compare its operands and return a Boolean based on whether the comparison is true or false. In the example below, the equality comparison operator is used to compare two numbers.

var x = 5;
var y = 5;
document.write(x == y); // prints true because x and y are equal

4. Logical Operators

Logical operators are typically used with Boolean values. They are used to determine the logic between its operands.

true && true;  // returns true
true && false; // returns false  
true || true;    // returns true
2 == 2 || 2 == 3; // returns true

Thanks for reading!!