ILILCode

Variables: let, const, and the values you keep

Store a value under a name, choose between let and const, and print something useful.

Lesson 1 of 23 in JavaScript8 min1 exercise

A variable is a name that points at a value. You create one with let when the value will change, and const when it will not. Prefer const by default — it makes your intent obvious and stops accidental reassignment.

Declaring and reading variables
const name = "Ada";
let score = 0;

score = score + 10;
console.log(name);   // "Ada"
console.log(score);  // 10

Names are case-sensitive. Use camelCase for variables and functions, UPPER_SNAKE_CASE for values that never change, and make names describe what they hold. totalPrice beats x.

Putting values inside strings

Template literals use backticks and ${...} to drop any expression into a string. They read far better than gluing strings together with +.

Two ways to build the same sentence
const user = "Grace";
const score = 42;

console.log("Hi " + user + ", you scored " + score + ".");
console.log(`Hi ${user}, you scored ${score}.`);

Try it

Edit the values and run it

main.js
console · output appears here

Exercises

Checked in your browser · hints and solutions included

01

Exercise

Build a greeting

Write a function makeGreeting(name) that returns the string Hello, <name>! — for example makeGreeting("Ada") returns "Hello, Ada!". Use a template literal.

exercise.js
console · run your code or check the answer