JavaScript Data Types & Conditionals - Guided Notes

CSCI 3403 - Web Programming | Day 19

Name: _________________________________ Date: _____________

📚 Today's Exercises: We'll complete 01_helloWorld and 02_addNumbers together in class. Exercises 03-05 are homework!

🎱 The Eight Data Types

Fill in the 7 Primitive Data Types:

  1. number - Example: _________________
  2. string - Example: _________________
  3. boolean - Values: _________ or _________
  4. _________________ - Variable declared but not assigned
  5. _________________ - Intentionally empty value
  6. symbol - Used for: _________________
  7. bigint - Used for: _________________

The 1 Non-Primitive Data Type:

8. _________________ - Includes arrays, functions, and more complex data

💡 Remember: Which data type is NOT primitive? _________________

📝 Working with Strings

Three Types of Quotes:

Type Symbol Special Feature
Single quotes _____ Basic string
Double quotes _____ Basic string
Backticks _____ Template literals - allows _________________

Embedding Variables in Strings:

let name = "Alice"; let age = 25; // Old way (concatenation): let message1 = "Hello, " + _______ + "! You are " + _______ + " years old."; // Modern way (template literals): let message2 = ___Hello, ${_______}! You are ${_______} years old.___;

🔧 Common String Methods

Fill in what each method does:

Method What it does Example Result
.length _________________________ "Hello".length = _____
.toUpperCase() _________________________ "hello".toUpperCase() = _________
.toLowerCase() _________________________ "HELLO".toLowerCase() = _________
.slice(start, end) _________________________ "JavaScript".slice(0, 4) = _________
.replace(old, new) _________________________ "Hi there".replace("Hi", "Hey") = _____________
.indexOf(text) _________________________ "Hello World".indexOf("World") = _____

🚪 Escape Characters

Complete the common escape sequences:

⚖️ Comparison Operators

Operator Name Example Result
== Equal (loose) 5 == "5" _________
=== Equal (strict) 5 === "5" _________
!= _________________ 5 != 3 _________
!== _________________ 5 !== "5" _________
> _________________ 10 > 5 _________
< _________________ 5 < 10 _________
⚠️ Best Practice: Always use _______ for comparisons unless you have a specific reason not to!

🔀 Logical Operators

The Three Logical Operators:

  1. && means _________ - Both conditions must be _________
  2. || means _________ - At least one condition must be _________
  3. ! means _________ - Flips the boolean value

Practice: What's the result?

let age = 25; let hasLicense = true; let isWeekend = false; age >= 16 && hasLicense // Result: _________ age < 16 || !hasLicense // Result: _________ isWeekend || age > 20 // Result: _________ !(age < 18) // Result: _________

✅❌ Truthy vs Falsy Values

❌ The 6 Falsy Values (memorize these!):

  1. _________
  2. _________ (the number)
  3. _________ (empty string)
  4. _________
  5. _________
  6. _________ (Not a Number)

✅ Everything else is Truthy!

Including:

  • "false" (the string)
  • "0" (the string)
  • [] (empty array)
  • {} (empty object)
  • Any number except 0
  • Any non-empty string

🚦 Conditionals

If/Else Statement Syntax:

if (_________condition_________) { // code if true } else if (_________another condition_________) { // code if second condition true } else { // code if all conditions false }

Switch Statement Syntax:

switch(_________variable_________) { case _________value1_________: // code for value1 _________; // Don't forget this! case _________value2_________: // code for value2 _________; default: // code if no match }

Ternary Operator Syntax:

let result = condition ? _____ifTrue_____ : _____ifFalse_____; // Example: let message = age >= 18 ? "Adult" : "Minor";

🪆 Nesting

What is nesting? _________________________________________

_________________________________________

⚠️ Warning: Try to keep nesting under _____ levels deep for readability!

💻 In-Class Exercises

Exercise 1: Hello World

Complete the function to return "Hello, World!":

function helloWorld() { _________________________ }

Exercise 2: Add Numbers

Complete the function to add two numbers:

function add(a, b) { _________________________ }

📚 Homework Exercises (Due Next Class)

Exercise Description Key Concepts
03_numberChecker Check properties of numbers _________________
04_mathEquations Solve math problems _________________
05_joinStrings Concatenate strings _________________

📋 Quick Reference

Common Patterns:

  • Check if exists: if (variable) {...}
  • Default value: let x = input || "default"
  • Range check: if (x >= min && x <= max)

Debugging Tips:

  • Use console.log() liberally
  • Check typeof variable
  • Test truthy/falsy with !!value

📝 Additional Notes

Use this space for any additional notes during the lecture: