Variables: let, const, and the values you keep
Store a value under a name, choose between let and const, and print something useful.
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.
const name = "Ada";
let score = 0;
score = score + 10;
console.log(name); // "Ada"
console.log(score); // 10Names 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 +.
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
Exercises
Checked in your browser · hints and solutions included
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.