CSCI 3403 - Web Programming
Day 19
Time to make our programs think! 🧠
number
- 42, 3.14string
- "Hello"boolean
- true/falseundefined
- not assignednull
- intentionally emptysymbol
- unique identifierbigint
- huge numbersobject
- Complex data[1, 2, 3]
{name: "John"}
// Single quotes let single = 'Hello World'; // Double quotes let double = "Hello World"; // Backticks (template literals) - SPECIAL! let name = "Alice"; let greeting = `Hello, ${name}!`; // "Hello, Alice!" let multiline = `This is a multiline string`;
let text = "JavaScript is awesome!"; // Length property console.log(text.length); // 22 // Change case console.log(text.toUpperCase()); // "JAVASCRIPT IS AWESOME!" console.log(text.toLowerCase()); // "javascript is awesome!" // Extract parts console.log(text.slice(0, 10)); // "JavaScript" console.log(text.substring(0, 10)); // "JavaScript" // Find and replace console.log(text.indexOf("is")); // 11 console.log(text.replace("awesome", "fun")); // "JavaScript is fun!"
let firstName = "John"; let lastName = "Doe"; let age = 25; let message = "Hello, my name is " + firstName + " " + lastName + " and I am " + age + " years old.";
let firstName = "John"; let lastName = "Doe"; let age = 25; let message = `Hello, my name is ${firstName} ${lastName} and I am ${age} years old.`;
// Quotes inside quotes let quote1 = "She said, \"Hello!\""; let quote2 = 'It\'s a beautiful day'; // Special characters let newLine = "First line\nSecond line"; let tab = "Column1\tColumn2\tColumn3"; let backslash = "C:\\Users\\Documents"; // Or use different quotes let easier = 'She said, "Hello!"'; let easier2 = "It's a beautiful day";
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to (loose) | 5 == "5" |
true |
=== |
Equal to (strict) | 5 === "5" |
false |
!= |
Not equal | 5 != 3 |
true |
> , < |
Greater/Less than | 10 > 5 |
true |
>= , <= |
Greater/Less or equal | 5 >= 5 |
true |
// AND (&&) - Both must be true let age = 25; let hasLicense = true; let canDrive = age >= 16 && hasLicense; // true // OR (||) - At least one must be true let isWeekend = false; let isHoliday = true; let dayOff = isWeekend || isHoliday; // true // NOT (!) - Flips the boolean let isRaining = false; let isSunny = !isRaining; // true // Combining operators let canEnter = (age >= 18 && hasTicket) || isVIP;
false
0
(zero)""
(empty string)null
undefined
NaN
(Not a Number)true
"false"
(string)"0"
(string)if ("hello") console.log("This runs!"); // Truthy if (0) console.log("This doesn't run"); // Falsy
// Basic if statement let score = 85; if (score >= 90) { console.log("A grade!"); } else if (score >= 80) { console.log("B grade!"); } else if (score >= 70) { console.log("C grade!"); } else { console.log("Keep studying!"); } // With logical operators let age = 19; let hasPermission = true; if (age >= 18 && hasPermission) { console.log("Access granted!"); } else { console.log("Access denied!"); }
let isLoggedIn = true; let userRole = "admin"; let hasPermission = true; if (isLoggedIn) { console.log("User is logged in"); if (userRole === "admin") { console.log("Welcome, Admin!"); if (hasPermission) { console.log("Full access granted"); } else { console.log("Limited admin access"); } } else { console.log("Welcome, User!"); } } else { console.log("Please log in"); }
let day = "Monday"; if (day === "Monday") { console.log("Start of work week"); } else if (day === "Friday") { console.log("TGIF!"); } else if (day === "Saturday" || day === "Sunday") { console.log("Weekend!"); } else { console.log("Regular day"); }
let day = "Monday"; switch(day) { case "Monday": console.log("Start of work week"); break; case "Friday": console.log("TGIF!"); break; case "Saturday": case "Sunday": console.log("Weekend!"); break; default: console.log("Regular day"); }
// Syntax: condition ? ifTrue : ifFalse // Instead of this: let age = 20; let message; if (age >= 18) { message = "You can vote!"; } else { message = "Too young to vote"; } // Use this: let age = 20; let message = age >= 18 ? "You can vote!" : "Too young to vote"; // More examples: let score = 85; let grade = score >= 90 ? "A" : score >= 80 ? "B" : "C"; let isLoggedIn = true; let greeting = `Welcome ${isLoggedIn ? "back" : "guest"}!`;
npm install
// Your task: Make this function return "Hello, World!" function helloWorld() { // Your code here } // Test with: npm test helloWorld
// Your task: Make this function add two numbers function add(a, b) { // Your code here } // Examples: // add(2, 3) should return 5 // add(-1, 1) should return 0 // add(0.1, 0.2) should return 0.3 (approximately) // Test with: npm test addNumbers
// Checking if something exists if (username) { console.log(`Hello, ${username}`); } else { console.log("Please enter a username"); } // Default values let name = inputName || "Anonymous"; // Range checking if (age >= 13 && age <= 19) { console.log("You're a teenager!"); } // Multiple conditions if (day === "Saturday" || day === "Sunday" || isHoliday) { console.log("No work today!"); } // Guard clauses (exit early) function processUser(user) { if (!user) return "No user provided"; if (!user.age) return "Age required"; if (user.age < 18) return "Must be 18+"; // Main logic here return "User processed!"; }
=
instead of ===
(assignment vs comparison)break
in switch statements// Debug with console.log! let value = getUserInput(); console.log("Value is:", value); console.log("Type is:", typeof value); console.log("Truthy?", !!value); if (value === "expected") { console.log("Match!"); } else { console.log("No match, value was:", value); }
npm test exerciseName
Next Class: Functions and Scope!
Questions?