JavaScript Foundations
Topic: Foundations
Concepts Covered: Variables (let,const), data types, console, operators
What is JavaScript?
Section titled “What is JavaScript?”JavaScript is a programming language primarily used for:
- Making web pages interactive
- Frontend development (running in the browser)
- Adding dynamic behavior to websites
Console & Debugging
Section titled “Console & Debugging”The console.log() function is your best friend for debugging and inspecting values:
console.log("Hello, World!"); // Outputs: Hello, World!console.log(42); // Outputs: 42Variables
Section titled “Variables”Variables are containers for storing data. JavaScript has different ways to declare variables:
Variable Declaration
Section titled “Variable Declaration”// Using let (can be reassigned)let monthlyRent = 200;console.log(monthlyRent); // 200
// Using const (cannot be reassigned)const yearlyRent = monthlyRent * 12;console.log(yearlyRent); // 2400Variable Naming Rules
Section titled “Variable Naming Rules”- Names can contain letters, digits,
$, and_ - First character must not be a digit
- Cannot use reserved keywords
// ❌ Invalidconst 12cohort = "Hello techies";let java-script = "programming language";
// ✅ Validconst cohort12 = 'Hello techies';let java_script = 'programming language';const $another_one = 'yep, this is valid';Naming Conventions
Section titled “Naming Conventions”// snake_caselet first_name = "John";
// camelCase (preferred in JavaScript)let monthlyRent = 200;Data Types
Section titled “Data Types”JavaScript has several basic data types:
1. Numbers
Section titled “1. Numbers”const num1 = 100;const num2 = 50;
// Arithmetic operators: +, -, *, /, **, %console.log(num1 + num2); // 150console.log(num1 - num2); // 50console.log(2 ** 3); // 8 (exponentiation)console.log(1 / 0); // Infinityconsole.log(num1 / "apple"); // NaN (Not a Number)2. Strings
Section titled “2. Strings”const fName = "John"; // Double quotesconst middleName = "Lennon"; // Single quotesconst lName = `Wick`; // Backticks (template literals)
const sentence = "This is 'Adewale'"; // Mixing quotes3. Booleans
Section titled “3. Booleans”const newYear = true;const isThis2024 = false;4. Null
Section titled “4. Null”Represents “nothing”, “empty” or “value unknown”:
const record = null;5. Undefined
Section titled “5. Undefined”Represents “value is not assigned”:
let undefinedVariable; // automatically undefinedconsole.log(undefinedVariable); // undefinedChecking Data Types
Section titled “Checking Data Types”Use the typeof operator:
console.log(typeof 2); // "number"console.log(typeof "Hello"); // "string"console.log(typeof true); // "boolean"console.log(typeof null); // "object" (this is a known quirk)console.log(typeof undefined); // "undefined"Operators
Section titled “Operators”Arithmetic Operators
Section titled “Arithmetic Operators”+Addition-Subtraction*Multiplication/Division**Exponentiation%Modulus (remainder)
Assignment Operators
Section titled “Assignment Operators”=Assign+=Add and assign-=Subtract and assign*=Multiply and assign/=Divide and assign
Browser Interactions
Section titled “Browser Interactions”JavaScript can interact with users through browser dialogs:
Displays a message to the user:
alert("Welcome to the JavaScript course");Prompt
Section titled “Prompt”Gets input from the user:
let age = prompt("How old are you?");console.log(`Your age is`, age);Confirm
Section titled “Confirm”Gets a yes/no response from the user:
let isOldEnough = confirm("Are you old enough to drive?");console.log(isOldEnough); // true or falseComments
Section titled “Comments”Comments help document your code:
// Single line comment
/* Multi-line comment can span multiple lines*/Key Takeaways
Section titled “Key Takeaways”- Use
letfor variables that can change - Use
constfor variables that won’t change console.log()is essential for debugging- JavaScript has dynamic typing (variables can hold different types)
- Always use meaningful variable names
- Comments make your code more readable
Practice Exercises
Section titled “Practice Exercises”- Create two variables with numbers and log their sum, difference, product, and exponent
- Experiment with dividing by zero and dividing strings by numbers
- Create variables for your favorite food, color, and hobby, then use them in a sentence