JavaScript Operators | JavaScript for Web Development - Software Development PDF Download

Introduction

JavaScript is a versatile programming language widely used in web development. As a beginner, understanding JavaScript operators is crucial for writing efficient and meaningful code. Operators are symbols that perform operations on values, such as mathematical calculations or comparisons. In this article, we will explore different types of JavaScript operators and provide examples to help you grasp their concepts.

Arithmetic Operators

Arithmetic operators are used for mathematical calculations. Let's explore each of them with examples:

Addition (+):

let result = 5 + 3;

console.log(result);  // Output: 8

The addition operator + adds two numbers together.

Subtraction (-):

let result = 10 - 4;

console.log(result);  // Output: 6

The subtraction operator - subtracts one number from another.

Multiplication (*):

let result = 3 * 6;

console.log(result);  // Output: 18

The multiplication operator * multiplies two numbers.

Division (/):

let result = 20 / 5;

console.log(result);  // Output: 4

The division operator / divides one number by another.

Modulus (%):

let result = 17 % 5;

console.log(result);  // Output: 2

The modulus operator % returns the remainder of dividing one number by another.

Increment (++) and Decrement (--):

let num = 5;

console.log(++num);  // Output: 6

console.log(--num);  // Output: 5

The increment operator ++ adds 1 to a variable, while the decrement operator -- subtracts 1 from a variable.

Assignment Operators

Assignment operators are used to assign values to variables. Here are some commonly used assignment operators:

Simple Assignment (=):

let num = 10;

console.log(num);  // Output: 10

The simple assignment operator = assigns the value on the right to the variable on the left.

Addition Assignment (+=):

let num = 5;

num += 3;

console.log(num);  // Output: 8

The addition assignment operator += adds the value on the right to the variable on the left and assigns the result to the variable.

Subtraction Assignment (-=):

let num = 10;

num -= 4;

console.log(num);  // Output: 6

The subtraction assignment operator -= subtracts the value on the right from the variable on the left and assigns the result to the variable.

Multiplication Assignment (*=):
let num = 3;

num *= 6;

console.log(num);  // Output: 18

The multiplication assignment operator *= multiplies the variable on the left by the value on the right and assigns the result to the variable.

Division Assignment (/=):

let num = 20;

num /= 5;

console.log(num);  // Output: 4

The division assignment operator /= divides the variable on the left by the value on the right and assigns the result to the variable.

Modulus Assignment (%=):

let num = 17;

num %= 5;

console.log(num);  // Output: 2

The modulus assignment operator %= calculates the remainder of dividing the variable on the left by the value on the right and assigns the result to the variable.

Comparison Operators

Comparison operators are used to compare values. They return a boolean value (true or false) based on the comparison. Let's look at some examples:

Equal to (==):

console.log(5 == 5);  // Output: true

console.log(5 == '5');  // Output: true

The equal to operator == compares whether two values are equal, performing type coercion if necessary.

Not equal to (!=):

console.log(5 != 3);  // Output: true

console.log(5 != '5');  // Output: false

The not equal to operator != compares whether two values are not equal, performing type coercion if necessary.

Strict equal to (===):

console.log(5 === 5);  // Output: true

console.log(5 === '5');  // Output: false

The strict equal to operator === compares whether two values are equal, without performing type coercion. It checks for both value and type.

Strict not equal to (!==):

console.log(5 !== 3);  // Output: true

console.log(5 !== '5');  // Output: true

The strict not equal to operator !== compares whether two values are not equal, without performing type coercion. It checks for both value and type.

Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=):
console.log(5 > 3);  // Output: true

console.log(5 < 3);  // Output: false

console.log(5 >= 3);  // Output: true

console.log(5 <= 3);  // Output: false

These operators compare whether one value is greater than, less than, greater than or equal to, or less than or equal to another value.

Logical Operators

Logical operators are used to combine or invert boolean values. Let's explore them:

AND (&&):

console.log(true && true);  // Output: true

console.log(true && false);  // Output: false

console.log(false && false);  // Output: false

The AND operator && returns true if both operands are true; otherwise, it returns false.

OR (||):

console.log(true || true);  // Output: true

console.log(true || false);  // Output: true

console.log(false || false);  // Output: false

The OR operator || returns true if either of the operands is true; if both operands are false, it returns false.

NOT (!):

console.log(!true);  // Output: false

console.log(!false);  // Output: true

The NOT operator ! inverts the boolean value. If the operand is true, it returns false; if the operand is false, it returns true.

Sample Problems

1. Create a program that checks if a given number is even or odd:

let num = 7;

if (num % 2 === 0) {

  console.log('The number is even.');

} else {

  console.log('The number is odd.');

}

2. Calculate the area of a rectangle with user input for length and width:

let length = parseFloat(prompt('Enter the length:'));

let width = parseFloat(prompt('Enter the width:'));

let area = length * width;

console.log('The area of the rectangle is: ' + area);

Conclusion

In this article, we have covered different types of JavaScript operators, including arithmetic, assignment, comparison, and logical operators. By understanding these operators, you can perform calculations, make comparisons, and control the flow of your code. Practice using operators in various scenarios to enhance your understanding and become proficient in JavaScript programming.

The document JavaScript Operators | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

Free

,

Semester Notes

,

Objective type Questions

,

JavaScript Operators | JavaScript for Web Development - Software Development

,

video lectures

,

Viva Questions

,

JavaScript Operators | JavaScript for Web Development - Software Development

,

study material

,

MCQs

,

pdf

,

Extra Questions

,

practice quizzes

,

Exam

,

Important questions

,

past year papers

,

Sample Paper

,

shortcuts and tricks

,

ppt

,

Summary

,

JavaScript Operators | JavaScript for Web Development - Software Development

,

Previous Year Questions with Solutions

;