ЁЯУЭ

JavaScript Operators and Conditional Statements

Jul 19, 2024

Operators and Conditional Statements

Beginning of Chapter 2

  • In Chapter 2 of the JavaScript series, we will focus on operators and conditional statements.
  • All past and future chapters will be available in the channel's playlist.

Real-life Examples

  • Example: Age requirement for voter ID and license.
  • Condition of passing marks to pass a class.
  • Similar conditions are implemented in code and websites.

Comments

  • The part of the code in JavaScript that does not get executed.
  • Useful for explaining the code.
  • Single Line Comments: //
  • Multiple Line Comments: /* ... */

Code Setup

  • index.html: Basic HTML5 boilerplate code
  • script.js: Script file
  • Connecting script.js to HTML using the script tag
  • Example: console.log('Hello World');

Arithmetic Operators

  • Increment Operator: ++
  • Decrement Operator: --

Operators

  • Plus: +
  • Minus: -
  • Multiply: *
  • Divide: /
  • Modulus: % (to get the remainder)
  • Exponentiation: ** (example: a ** b)*

Unary Operators

  • Increment: a++ <=> a = a + 1
  • Decrement: a-- <=> a = a - 1

Assignment Operators

  • = : Assigning a value to a variable
  • **+=, -=, *=, /=, %=, *= : Compound assignment operators

Illustrative Example

  • a += 4 signifies a = a + 4

Comparison Operators

  • Equal to: ==
  • Not equal to: !=
  • Strict equal to: ===
  • Strict not equal to: !==
  • Greater than: >
  • Less than: <
  • Greater than or equal to: >=
  • Less than or equal to: <=

Logical Operators

  • AND: &&
  • OR: ||
  • NOT: !

Conditional Statements

  • Using conditional statements to implement some conditions in the code.

If Statement

  • Checking a condition, if the statement is true the code block is executed. if (condition) { // Code to be executed if condition is truthy }

Else Statement

if (condition) { // Code to be executed if condition is truthy } else { // Code to be executed if condition is falsy }

Else If Statements

if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if none of the above conditions are true }

Ternary Operator

  • Syntax: condition ? expr1 : expr2
  • Example: let result = (age >= 18) ? 'Adult' : 'Not Adult';

MDN Documentation

  • A good resource for reading programming documentation.

Larger Examples and Practice Questions

  • Checking multiples of a number
  • Student Grading System

Practice Questions

  1. Take a number from the user and check whether it is a multiple of 3 or 5.
  2. Assign a grade based on the student's score:
    • 90-100: A
    • 70-89: B
    • 60-69: C
    • 50-59: D
    • 0-49: F

Plan

  1. Take input from the user via prompt
  2. Check if-else conditions
  3. Display the result at the end

Practice Code:

// Multiple Check let number = prompt('Enter a number:'); if (number % 3 === 0) { console.log(number + ' is a multiple of 3'); } else if (number % 5 === 0) { console.log(number + ' is a multiple of 5'); } else { console.log(number + ' is not a multiple of 3 or 5'); } // Grading System let score = prompt('Enter your score:'); let grade; if (score >= 90 && score <= 100) { grade = 'A'; } else if (score >= 70 && score <= 89) { grade = 'B'; } else if (score >= 60 && score <= 69) { grade = 'C'; } else if (score >= 50 && score <= 59) { grade = 'D'; } else if (score >= 0 and score <= 49) { grade = 'F'; } console.log('According to your score, your grade is ' + grade);