Skip to content

Beginner · JavaScript basics · topic 19 of 64 · 25 min

Values, types and variables

Explanation

A program works with values: numbers, text (strings), true/false (booleans) and a few others. A variable is a name that holds a value so you can use it later. In modern JavaScript, use const for values that do not change and let for values that do.

What is a program?

A program is a list of instructions the computer follows, one after another, from top to bottom. JavaScript instructions are called statements, and each usually ends with a semicolon.

javascript
console.log('Namaste!');
console.log(2 + 3); // 5
console.log prints a value in the Console

Types of values

TypeExamplesUsed for
number42, 3.5, -10Prices, ages, counts
string'Asha', "Pune"Any text, written in quotes
booleantrue, falseYes/no answers: isLoggedIn
undefinedundefinedA variable that has no value yet
nullnull"Empty on purpose", set by you

Arrays (lists) and objects (groups of named values) are also values; you will meet them soon. The typeof operator tells you the type: typeof 42 is "number", typeof "hi" is "string".

Variables: names for values

Think of it like this: Think of a variable as a labelled steel dabba in a kitchen. The label is the name ("sugar"), and what is inside is the value. A const dabba is sealed after you fill it; a let dabba can be emptied and refilled.
javascript
const shopName = 'Sharma Kirana';
let itemsInCart = 0;
itemsInCart = itemsInCart + 1; // let can change
// shopName = 'Other';  // Error: const cannot be reassigned
console.log(shopName, itemsInCart); // Sharma Kirana 1
const for fixed values, let for values that change

Use const by default. Switch to let only when you really need to change the value. Do not use var: it is the old way and has confusing rules about where the variable can be seen.

Naming rules

  • Names can contain letters, digits, _ and $, but cannot start with a digit.
  • JavaScript is case-sensitive: total and Total are different variables.
  • Use camelCase: first word lowercase, each next word starts with a capital (totalPrice, isPaid).
  • Choose names that explain: priceInRupees is better than p.

Strings and numbers mix carefully

javascript
console.log(10 + 5);      // 15
console.log('10' + 5);    // '105'  (text joined, not added!)
console.log(Number('10') + 5); // 15
console.log(typeof '10'); // 'string'
The + sign joins strings; convert text to a number first
Tip: Values typed in a form always arrive as strings. Convert them with Number() before doing maths.
Careful: Comments start with // (one line) or sit between /* and */. Use them to explain why, not to repeat what the code already says.

Watch the lesson

Values, types and variables · 1/6

Program = instructions in order

A program is a list of instructions the computer runs from top to bottom.

Prefer a video by a teacher? Find free videos on “javascript variables let const data types for beginners” on YouTube ↗

In real life: A shopping cart counter

On a grocery app, const deliveryFee = 25 never changes during your visit, while let cartCount starts at 0 and goes up each time you tap "Add". The shop name is a string, the fee a number, and isFreeDelivery a boolean that becomes true when the cart total crosses 499 rupees.

Practice (0/2 done)

Task 1 · Code · Javascript

Create a const called city with the value "Jaipur", a let called visitors with the value 0, then add 3 to visitors (so it becomes 3).

Task 2 · Code · Javascript

Write a function addStrings(a, b) that receives two numbers written as strings (like "10" and "5") and returns their sum as a number (15).

Quick quiz

Question 1

1.Which keyword should you use for a value that never changes?

Question 2

2.What does '5' + 5 give in JavaScript?

Question 3

3.What is typeof true?

Question 4

4.Which variable name follows camelCase?

Question 5

5.What value does a declared variable have before you give it one?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Typography: fonts and readable text