Variables and Data Types
In JavaScript, variables allow us to store and manipulate data. They are like containers that hold different values. Before we can use a variable, we need to declare it using the let
or const
keyword.
For example, let's declare some variables to store information about a basketball player:
JAVASCRIPT
1const playerName = 'Kobe Bryant';
2const points = 81;
3const team = 'Los Angeles Lakers';
In this example, we have declared three variables:
playerName
to store the name of the player, which is a stringpoints
to store the number of points scored by the player, which is a numberteam
to store the name of the team the player belongs to, which is also a string
We can use the variables in our code to perform operations or display information. For example, we can log the values of these variables to the console using the console.log()
function:
JAVASCRIPT
1console.log('Player Name:', playerName);
2console.log('Points:', points);
3console.log('Team:', team);
When we run this code, it will output the following:
SNIPPET
1Player Name: Kobe Bryant
2Points: 81
3Team: Los Angeles Lakers
xxxxxxxxxx
const playerName = 'Kobe Bryant';
const points = 81;
const team = 'Los Angeles Lakers';
console.log('Player Name:', playerName);
console.log('Points:', points);
console.log('Team:', team);
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment