Lesson 2 of 10

JavaScript Foundations

Lesson 2: Variables and Types

Now we store real values in variables and print them clearly.

Learning Goals

Example

When we write this, we mean each variable stores one piece of information.

// Save as lesson2.js
let internName = "Patricia";
let week = 1;
let isPresent = true;

week = week + 1;

console.log(internName);
console.log(week);
console.log(isPresent);
console.log(typeof internName);
console.log(typeof week);

When we write this, we mean:

Keep it simple: use only let for now in this track.
Tip: Run node lesson2.js in your terminal.

Real Example: Internship Attendance

Imagine you are tracking one intern's week details.

let intern = "Asha";
let daysPresent = 4;
let submittedTask = false;

console.log(intern);
console.log(daysPresent);
console.log(submittedTask);

That code means: we are storing a person's name, attendance count, and task status.

Deeper Explanation

Variables are how your program remembers information. In real projects, this is everything: user names, scores, prices, login status, dates, and many more values. If you do not store values properly, you cannot make decisions, calculate results, or show useful output. That is why this lesson is foundational.

Notice that we also check data type with typeof. This is a beginner superpower. If your app behaves strangely, checking type often reveals the problem quickly. For example, adding text and adding numbers are different operations. Seeing the type helps you debug with confidence instead of guessing.

Common Beginner Mistakes

  1. Mixing text and numbers without noticing: "2" is not the same as 2.
  2. Changing the wrong variable name due to spelling differences.
  3. Forgetting that true and false are not in quotes.

Practice

  1. Create a variable for your school name.
  2. Create a variable for your age and add 1.
  3. Create a true/false variable for isMorning.
  4. Print the type of each variable using typeof.

Homework

Build a file called profile.js with 6 variables: name, age, city, course, isOnline, and week. Update one value, then print all values and their types.

Answer Guide
let name = "Brian";
let age = 20;
let city = "Jinja";
let course = "JavaScript Basics";
let isOnline = true;
let week = 1;

week = week + 1;

console.log(name, typeof name);
console.log(age, typeof age);
console.log(city, typeof city);
console.log(course, typeof course);
console.log(isOnline, typeof isOnline);
console.log(week, typeof week);
← Previous Lesson Next Lesson →