Javascript Operators & Conditional Statements PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides examples of JavaScript operators and conditional statements. It explains arithmetic, assignment, and comparison operators. The document includes example code and output.
Full Transcript
TERM 2 | COMPUTER TECH | ETA REVIEWER LESSON 1: JAVASCRIPT OPERATORS Operators are special symbols that perform operations on variables and values. 1. Arithmetic Operators - used to perform mathematical operations like addition, subtraction, multiplication, etc. Example Code: v...
TERM 2 | COMPUTER TECH | ETA REVIEWER LESSON 1: JAVASCRIPT OPERATORS Operators are special symbols that perform operations on variables and values. 1. Arithmetic Operators - used to perform mathematical operations like addition, subtraction, multiplication, etc. Example Code: var a = 2; var b = 5; document.write(“Sum: “ + (a+b)); //Output: Sum: 7 2. Assignment Operators (=) - are used to assign values to variables. Example Code: var a = 10; var b = 20; a+=b // a = a + b // a = 10 + 20 document.write(a) //Output: 30 3. Comparison Operators - compare two values/variables and return a boolean result: True or False. Operator Meaning Note == Is equal to It doesn’t consider the data type, as long as the characters are the same, the boolean value is True. === Is strictly equal to Strictly considers the data type. != Not equal to It doesn’t consider the data type, as long as the characters are the same, the boolean value is True. !== Is strictly not equal to Strictly considers the data type. > Greater than < Less than >= Greater than or equal to 55); // true document.write( c || d) //Output: true var e = (75 == 100); // false document.write(!(e)); //Output: false 5. String Operator (+) - used to link two or more string values together. Example Code: var name = “Miguel”; document.write(“My name is “ + name + “.”) //Output: My name is Miguel. LESSON 2: JAVASCRIPT CONDITIONAL STATEMENTS If statement - to specify a block of JavaScript code to be executed if a condition is true. Else statement - to specify a block of code to be executed if the condition is false. Else if statement - to specify a new condition if the first condition is false. Example Code: #1 var name = “Juan”; if (name == “Juan”) { document.write(“Match”); } else{ document.write(“Not a match”) } //Output: Match #2 if (BMI < 18.5) { document.write("Underweight”); } else if (BMI >= 18.5 && BMI