Week 1
JavaScript Basics
Variables and Data Types
Learn how to store and manipulate data in JavaScript!
Learning Objectives
- Understand what variables are
- Learn about different data types
- Practice declaring and using variables
Explanation
Variables are like containers that store data. In JavaScript, you can create variables using let, const, or var:
- let: Declares a variable that can be reassigned (modern approach).
- const: Declares a constant variable that cannot be reassigned (modern approach).
- var: Older way to declare variables (try to use let and const instead).
JavaScript has several data types:
1. Primitive Types:
- String: Text (e.g., "Hello")
- Number: Numbers (e.g., 42, 3.14)
- Boolean: true or false
- undefined: Variable declared but not assigned a value
- null: Intentional absence of any value
- Symbol: Unique and immutable values (advanced)
2. Object Types:
- Object: Collections of related data
- Array: Ordered lists
- Function: Reusable blocks of code
JavaScript is "dynamically typed," which means variables can change types. But it's generally good practice to keep a variable's type consistent.
Code Example
// Variable declarations let name = "Luke Skywalker"; // String let age = 19; // Number let isJedi = true; // Boolean // Constants (can't be reassigned) const GRAVITY = 9.8; // Multiple declarations let planet = "Tatooine", system = "Outer Rim", hasWater = false; // Changing variable values name = "Luke"; // This works with let // GRAVITY = 10; // This would cause an error with const // Different types of data let jediKnight = { // Object name: "Obi-Wan", age: 38, lightsaberColor: "blue" }; let droids = ["R2-D2", "C-3PO", "BB-8"]; // Array // Checking types console.log(typeof name); // "string" console.log(typeof age); // "number" console.log(typeof isJedi); // "boolean" console.log(typeof jediKnight); // "object" console.log(typeof droids); // "object" (arrays are objects in JS)
Try It Yourself!
Try creating variables to store information about yourself, like name, age, and favorite color!
JavaScript Editor
Mini Quiz
Test your knowledge with these quick questions!
1. Which keyword is used to declare a variable that cannot be reassigned?
let
var
const
fixed
2. What will typeof [1, 2, 3] return?
'array'
'number'
'object'
'list'
Your Progress
Lesson ProgressIn Progress
Est. time: 15-20 min
75 points
Module Progress
JavaScript Fundamentals2/10 completed
Complete all lessons to earn the Code Fundamentalist badge!
Rewards
Complete this lesson
Earn 75 points
Complete this module
Earn the Code Fundamentalist badge