Variables and Values

Computer can not only compute numbers, but also remember things. Variables work like containers that can hold values such as numbers, strings, or other types of data.

Simply put, a variable is a name that refers to a value.

To create a variable in JavaScript, we need to use the let keyword to declare (or define) a variable first.

Start a new line, type in and run the following code:

let x; 
x = 10;
console.log(x);

Console result:

10

The first line declares a variable named x. The second line assigns an integer 10 to the variable x. It now holds value 10. When we use it as input data for console.log() function, its value 10 is printed out.

We can also declare and assign value to a variable in one line:

let age = 15;

Assignment of values to variables

let variable_name = value;

Summary:

  • let keyword declares a variable.
  • = sign is the assignment operator.
  • The value might be a string, a number or other types of data.

Variable names: Variable names may contain letters, digits or underscores, but cannot start with a digit.

Examples of valid variable names: y, a3, my_number, _stuff.

Variable names are case-sensitive, which means a and A are two different variables.

Next let's assign a string value "Bob" to a variable name:

let name = "Bob";
console.log(name);

Console result:

Bob

As we continue the lesson, you'll see JavaScript has many other types of data. We can assign them to variables just like strings or integers.

results matching ""

    No results matching ""