Welcome to this tutorial on exploring the different operators in JavaScript! The goal of this tutorial is to introduce you to the various types of operators in JavaScript, including arithmetic, comparison, and logical operators. You'll learn how to use these operators to manipulate data, make decisions, and add complexity to your JavaScript programs.
By the end of this tutorial, you should be able to:
There are no specific prerequisites for this tutorial, but a basic understanding of JavaScript syntax and data types will be beneficial.
In JavaScript, operators are used to perform operations on variables and values. Let's dive into each type:
Arithmetic operators are used to perform mathematical operations:
+
Addition-
Subtraction*
Multiplication/
Division%
Modulus (remainder)++
Increment--
DecrementExample:
let x = 10;
let y = 5;
console.log(x + y); // Addition, Expected output: 15
console.log(x - y); // Subtraction, Expected output: 5
console.log(x * y); // Multiplication, Expected output: 50
console.log(x / y); // Division, Expected output: 2
console.log(x % y); // Modulus, Expected output: 0
console.log(x++); // Increment, Expected output: 10 (returns the value before incrementing)
console.log(x); // Expected output: 11 (value after incrementing)
console.log(y--); // Decrement, Expected output: 5 (returns the value before decrementing)
console.log(y); // Expected output: 4 (value after decrementing)
Comparison operators are used to compare two values:
==
Equal to===
Equal value and equal type!=
Not equal!==
Not equal value or not equal type>
Greater than<
Less than>=
Greater than or equal to<=
Less than or equal toExample:
let a = 10;
let b = '10';
console.log(a == b); // Expected output: true
console.log(a === b); // Expected output: false
console.log(a != b); // Expected output: false
console.log(a !== b); // Expected output: true
console.log(a > b); // Expected output: false
console.log(a < b); // Expected output: false
console.log(a >= b); // Expected output: true
console.log(a <= b); // Expected output: true
Logical operators are used to determine the logic between variables or values:
&&
Logical and||
Logical or!
Logical notExample:
let c = 5;
let d = 10;
console.log(c < 10 && d > 1); // Expected output: true
console.log(c == 5 || d == 5); // Expected output: true
console.log(!(c == d)); // Expected output: true
This tutorial covered the basics of JavaScript operators. You learned how to use arithmetic, comparison, and logical operators to perform operations and make decisions in your JavaScript programs.
As a next step, try to use these operators in more complex ways and combine them in your scripts. You can also explore other JavaScript concepts such as loops, conditions, and functions.
Solutions:
1.
let e = 10;
e++;
console.log(e > 10); // Expected output: true
2.
let f = '5';
let g = 5;
console.log(f === g); // Expected output: false
3.
let h = 7;
console.log(h < 5 || h > 10); // Expected output: false
These exercises should give you a good start on understanding JavaScript operators. Keep practicing and exploring different combinations and scenarios. Happy coding!