Exploring JavaScript Operators

Tutorial 4 of 5

Introduction

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:

  • Understand the different types of operators in JavaScript
  • Use these operators in your code
  • Write more complex and dynamic JavaScript programs

There are no specific prerequisites for this tutorial, but a basic understanding of JavaScript syntax and data types will be beneficial.

Step-by-Step Guide

In JavaScript, operators are used to perform operations on variables and values. Let's dive into each type:

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (remainder)
  • ++ Increment
  • -- Decrement

Example:

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

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 to

Example:

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

Logical operators are used to determine the logic between variables or values:

  • && Logical and
  • || Logical or
  • ! Logical not

Example:

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

Summary

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.

Practice Exercises

  1. Write a program that increments a variable by 1 and then checks if the new value is greater than 10.
  2. Write a program that compares two variables and returns true if they are of the same type and value, and false otherwise.
  3. Write a program that checks if a variable is less than 5 or greater than 10.

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!