Variables

You can imagine variables as boxes where you can store a value and later use it. Variables are memory for your program.

With variables, we can perform the following operations:

  • Create a variable with a chosen name.
  • Assign or overwrite a variable's value.
  • Retrieve (use) the variable's value.

We can create variables in JavaScript using 3 keywords:

  • let
  • const
  • var

In most cases, we will create variables using the let keyword, as it is the most flexible.

For example:

let date = "11.2.2024";
console.log(date);

This will print:

11.2.2024

In the example, we used the let keyword to create a variable named date and assigned it the value "11.2.2024" using the = operator. We then used this value in the console.log command to print its value.

Instructions

Create a variable firstName using the let keyword and assign it the value "John".

On the next line, create a variable surname using the let keyword and assign it the value "Doe".

On the next line, print the value of the firstName variable using the console.log command.

On the next line, print the value of the surname variable using the console.log command.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/10

Variables | Start Coder