🟨 JavaScript tutorial for beginners · Class 8 · Lesson 2 of 18 · Free
Variables & Types – let, const and Data Types in JavaScript
⏱ About 30 minutes · ▶ run real JavaScript here — nothing to install · no sign-in
In this lesson you will
- Create variables with let and const and choose the right one
- Check any value's type with typeof
- Convert text to numbers so your maths comes out right
Kabir built a marks calculator. He typed 40 and 35, and it said the total was "4035"! The problem was not maths — it was the type of the values. In this lesson you learn how JavaScript stores values and how to stop surprises like this.
What is a variable in JavaScript and when do I use let or const?
In Lesson 1 you met variables: named boxes that hold values. You create a box once with let or const. After that you use the name without let.
Use const for values that stay the same, like a student's roll number. Use let for values that change, like pocket money. A good habit: start with const, and switch to let only if the value must change.
Old code also uses var. It works differently in tricky ways, so modern JavaScript uses let and const instead.
▶const name = "Meera";2const rollNo = 17;3let pocketMoney = 200;4console.log(name, "has ₹" + pocketMoney);56pocketMoney = pocketMoney - 50; // bought a book7console.log(name, "has ₹" + pocketMoney);89pocketMoney = pocketMoney + 100; // Diwali gift10console.log(name, "has ₹" + pocketMoney);
No variables yet.
▌✏️ Change this code and run it yourself
Meera has ₹200Meera has ₹150Meera has ₹250
💡 Variable names can use letters, digits, _ and $, but cannot start with a digit. Use camelCase: pocketMoney, not pocket money.
What are the data types in JavaScript?
Every value has a type. The main ones are: number (42, 3.5), string ("Hello"), boolean (true/false), undefined (no value yet) and null (empty on purpose).
The typeof operator tells you the type of any value. It gives the answer as a string.
JavaScript has only one number type. Whole numbers and decimals are both "number".
numbernumberstringbooleanstring
💡 Look at the last line: "13" in quotes is a string, not a number!
Why does "5" + 3 give "53" in JavaScript?
The + sign has two jobs. With two numbers, it adds. If either side is a string, it joins them as text. So "5" + 3 becomes "53".
The other signs (-, *, /) only work with numbers. So JavaScript quietly turns "10" into 10 first. This mix-up is a very common bug.
The fix: turn text into a number with Number(). Turn a number into text with String().
▶const a = "5";2const b = 3;3console.log(a + b);4console.log(a - b);56const fixed = Number(a);7console.log(fixed + b);8console.log(typeof fixed);910console.log(Number("hello"));
No variables yet.
▌✏️ Change this code and run it yourself
5328numberNaN
💡 NaN means "Not a Number". You get it when text cannot become a number.
What is the difference between undefined and null in JavaScript?
If you make a variable with let and give it no value, it holds undefined. It means "nothing has been put here yet".
null is a value you put in on purpose. It means "this is empty, and I know it". For example, no one has won the match yet.
One famous oddity: typeof null says "object". This is an old mistake in JavaScript that can never be changed now. Just remember it.
▶let homework;2console.log(homework);3console.log(typeof homework);45let winner = null;6console.log(winner);7console.log(typeof winner);89winner = "Team Blue";10console.log(winner);
No variables yet.
▌✏️ Change this code and run it yourself
undefinedundefinednullobjectTeam Blue
How do I store many values together in JavaScript?
Sometimes one value is not enough. An array is a list in square brackets: ["Maths", "Science"]. An object groups named values in curly brackets: { name: "Asha", age: 12 }.
Both are type "object". You will learn arrays in Lesson 7 and objects in Lesson 10. For now, just see what they look like.
Template literals are strings in backticks ` `. Inside them, ${ } puts a variable straight into the text — much neater than many + signs.
["Maths", "Science", "English"]{ name: "Asha", age: 12, class: 8 }object objectAsha is 12 years old.
Why is a value from a web page input always a string?
Text boxes on a web page always give you a string, even if the user types digits. That is exactly Kabir's "4035" bug.
Type two marks below and click both buttons. The first adds strings. The second uses Number() first and gets the right total.
📄 HTML of the page (tap to see / edit)
string 40
💡 The console shows: the value is a string, even though it looks like a number.
🤔 Predict the output
Think like JavaScript: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
console.log(typeof "42");
Question 2. What does this print?
console.log("10" - 2);
console.log("10" + 2);Question 3. What does this print?
let marks; console.log(marks); marks = 90; console.log(typeof marks);
🧩 Your challenge
A shop sells a samosa for ₹15. The quantity arrives as text, like from a web form: const qtyText = "4";. Turn it into a number, work out the bill, and print the type and the bill. Expected output: number Bill for 4 samosas: ₹60
number Bill for 4 samosas: ₹60
⚠️ Common mistakes (and how to fix them)
❌ Wrong
const total = 0; total = total + 50; console.log(total);
TypeError: Assignment to constant variable.
✅ Right
let total = 0; total = total + 50; console.log(total);
const means the box can never get a new value. A running total changes, so it needs let.
❌ Wrong
console.log(score); let score = 10;
ReferenceError: Cannot access 'score' before initialization
✅ Right
let score = 10; console.log(score);
You cannot use a let or const variable before the line that creates it. Create the variable first, then use it.
❌ Wrong
const marks = prompt("Your maths marks?");
console.log("With 5 bonus marks:", marks + 5);No error, but it prints 805 instead of 85. prompt() always returns a string, so + joins "80" and 5 as text.
✅ Right
const marks = Number(prompt("Your maths marks?"));
console.log("With 5 bonus marks:", marks + 5);Wrap the answer in Number() so + adds instead of joining.
📌 Remember
- Use const for values that never change and let for values that do; avoid var.
- The main types are number, string, boolean, undefined and null.
- typeof tells you a value's type, as a string like "number".
- + joins text if either side is a string; use Number() to turn text into a number.
- Values from prompt() and page inputs are always strings.
- Template literals `like ${this}` put variables inside text neatly.
Finished the quiz and the challenge?
Questions students ask
What is the difference between let, const and var in JavaScript?
let makes a variable you can change. const makes one you cannot give a new value. var is the old way; it ignores { } blocks and can cause confusing bugs, so modern code uses let and const.
Why does typeof null return "object"?
It is a bug from the very first version of JavaScript in 1995. Fixing it would break old websites, so it stays. null is really its own type meaning "empty on purpose".
What does NaN mean in JavaScript?
NaN stands for "Not a Number". You get it when maths fails, for example Number("hello"). Strangely, typeof NaN is "number".
How do I convert a string to a number in JavaScript?
Use Number("42"), which gives 42. parseInt("42px") also works and reads digits from the start of the text. If the text is not a number, you get NaN.