Code / JavaScript from Zero / Unit 1: Values and variables

Your first program आपका पहला प्रोग्राम

Lesson 1 of 15 · about 10 minutes · console.log, let and const.

⚡ 0 XP

Step 1 of 2

Talking to the console

"Pune"city

A program is a list of instructions that the computer follows from top to bottom. console.log(...) prints a message in the console, so you can see what your code is doing.

A variable is a labelled box that stores a value. Make one with let:

  • let city = "Pune"; puts the text Pune in a box called city
  • console.log(city); prints Pune

Text always goes inside quotes. Join pieces of text with +.

Example
let city = "Pune";
console.log("Namaste from " + city);

Prints: Namaste from Pune

🎯 Challenge: Say namaste +10 XP

Create a variable name with your name, then print Namaste, followed by the name, e.g. Namaste, Asha.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • A variable called name holds some text
  • The console shows “Namaste, <name>”
Lesson notes (text version)

1. Talking to the console

A program is a list of instructions that the computer follows from top to bottom. console.log(...) prints a message in the console, so you can see what your code is doing.

A variable is a labelled box that stores a value. Make one with let:

  • let city = "Pune"; puts the text Pune in a box called city
  • console.log(city); prints Pune

Text always goes inside quotes. Join pieces of text with +.

                let city = "Pune";
console.log("Namaste from " + city);
              

Challenge: Say namaste

Create a variable name with your name, then print Namaste, followed by the name, e.g. Namaste, Asha.

2. let or const?

Use let for a value that will change, and const for a value that stays the same.

  • const shop = "Sharma Tea Stall"; — the shop name never changes
  • let cups = 12; and later cups = cups + 5; — the count of cups changes

Notice: when you change a let variable you write its name without let again. If you try to change a const, JavaScript stops with an error.

Good habit: start with const, and switch to let only when the value must change.

                const shop = "Sharma Tea Stall";
let cups = 12;
cups = cups + 5;
console.log(shop, "sold", cups, "cups");
              

Challenge: The chai stall

Make a const called shopName with your stall’s name. Make a let called cups that starts at 12. Then 5 more cups are sold: add 5 to cups. Finally print cups (it should show 17).

Search by task, format or tool name.