click below
click below
Normal Size Small Size show me how
JS - Week 2
WTWD 310 - JavaScript
| Term | Definition | Example |
|---|---|---|
| If/Else Statement | A control structure that executes a block of code if a specified condition is true. If the condition is false an optional else block can execute. | if (age >= 18) { console.log('You are an adult.'); } else { console.log('You are a minor.'); } |
| Loop | A control structure that repeatedly executes a block of code as long as a specified condition is true. Common loops include for while and do...while. | for (let i = 0; i < 5; i++) { console.log(i); } |
| Method | A function that is a property of an object. Methods are used to define behaviors of objects. | const person = { name: 'John', greet: function() { return 'Hello ' + this.name; } }; console.log(person.greet()); |
| Array | A data structure that can hold multiple values at once. Arrays are ordered and indexed starting from zero. | let fruits = ['apple', 'banana', 'cherry']; console.log(fruits[0]); // Output: apple |
| Constructor | A special function used to create and initialize objects. Constructors are often used with classes to set initial properties. | function Person(name, age) { this.name = name; this.age = age; } let person1 = new Person('Alice', 30); console.log(person1.name); // Output: Alice |
| Conditional | An expression that evaluates to true or false used to determine which code should be executed. Commonly used with if statements. | let isAdult = age >= 18; if (isAdult) { console.log('You are an adult.'); } |
| Switch Statement | A control structure that executes one block of code from multiple choices based on the value of an expression. | let color = 'red'; switch (color) { case 'red': console.log('Color is red'); break; case 'blue': console.log('Color is blue'); break; default: console.log('Color is not red or blue'); } |
| Alert() | A built-in JavaScript function that displays an alert dialog with a specified message and an OK button. | alert('This is an alert message!'); |
| Comparison Operators | Operators used to compare two values and return a boolean result (true or false) based on the comparison. These operators are essential for making decisions in the code often used in conditional statements like if else and loops. | let a = 10; let b = 5; console.log(a > b); // Output: true console.log(a == b); // Output: false console.log(a !== b); // Output: true |
| Math Methods | Built-in methods of the JavaScript Math object that perform mathematical operations. These methods provide functionalities for common mathematical tasks such as rounding numbers generating random numbers and finding maximum or minimum values. | let num = -5; console.log(Math.abs(num)); // Output: 5 let randomNum = Math.random(); console.log(randomNum); // Output: a number between 0 and 1 |
| Logical Operator | Operators used to combine or invert boolean values and expressions. The three main logical operators in JavaScript are && (AND) || (OR) and ! (NOT). Logical operators are used to build complex conditional statements by combining multiple conditions. | let a = true; let b = false; // AND (&&) operator console.log(a && b); // false // OR (||) operator console.log(a || b); // true // NOT (!) operator console.log(!a); // false |