Skip to content

JavaScript Variables, Values and Types: let, const, strings and numbers

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

HTML gives a page its structure and CSS gives it its look. JavaScript gives it behaviour: the VAT calculator that updates as you type, the form that warns you before you send a wrong phone number, the menu that opens on a phone. Every one of those starts with the same small step: storing a value, such as a price or a name, and doing something with it. This lesson teaches that step properly, including the details that trip up beginners for months.

Quick answer

A variable is a named box for a value. Use const for a value that will not be reassigned, such as const vatRate = 0.05;, and let for one that changes, such as let total = 0;. Avoid the old var. The main value types are strings (text in quotes), numbers, booleans (true or false), null and undefined. Build text with template literals, written between backticks with values in ${...}, check a type with typeof, and print anything with console.log().

25 min read and practice · Content checked on 5 November 2026

In this lesson you will

  • Run JavaScript in a page and read its output in the console
  • Choose between const and let, and explain why var is avoided
  • Work with strings, numbers and booleans, and build text with template literals
  • Handle decimal money values without surprise rounding errors

Where JavaScript runs and how to see its output

JavaScript is the only programming language every web browser runs. You add it to a page with a <script> element, usually just before </body> or in a separate file linked with <script src="app.js" defer></script>. The browser reads the code from top to bottom and runs each statement in order. JavaScript also runs outside browsers, for example in Node.js on servers, but this course starts in the browser because you already have one.

console.log() prints a value to the console, a panel for developers. In your own browser, open the developer tools (F12, or Ctrl+Shift+I, or Cmd+Option+I on a Mac) and choose Console. On this site, every example has a Console box under the result that shows what the code printed and any error with its line number. Errors are normal: read the message, find the line, fix it and run again. That loop is most of programming.

Try it: Your first lines of JavaScript
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

The console shows three lines. 2 + 3 adds numbers and gives 5, but "2" + "3" joins two pieces of text and gives 23. The quotes make all the difference.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 2 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between let and const in JavaScript?

Both create block-scoped variables. A const cannot be reassigned after it is created, while a let can. Use const by default and let for values that must change, such as a counter or a running total.

Should I still use var in JavaScript?

No, not in new code. var is function-scoped and hoisted, which causes confusing bugs. let and const fix that and are supported in every current browser. You only need to recognise var when reading older code.

Why does 0.1 + 0.2 not equal 0.3 in JavaScript?

Numbers are stored in binary floating point, as in most languages, and 0.1 and 0.2 have no exact binary form. The tiny error shows when you print the sum. For money, calculate with full precision and round once with toFixed(2) or Intl.NumberFormat when displaying.

Is JavaScript the same as Java?

No. They are different languages with different uses, and the similar name came from marketing in the 1990s. JavaScript runs in every browser and in Node.js; Java is mostly used for large back-end systems and Android apps.

Where do I see console.log output?

In the browser’s developer tools, in the Console tab: press F12, or Ctrl+Shift+I on Windows, or Cmd+Option+I on a Mac. In the lessons on this site, the Console box under each result shows the same output without opening anything.

Sources

Facts in this lesson were checked against these official pages on 5 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.