Declaring Variables
A computer program is a set of rules that tell a computer what to do.These rules are written in a special language called programming language
, and they are known as statements
.
Now, we can talk about JavaScript variables
. Variables are like containers that hold information. Think of them as labeled boxes where you can store numbers, words, or other types of data.
When you create a variable, you give it a name, so you can easily find and use it later in your program. For example, if you want to keep track of someone's age, you could create a variable called age
and store the number in it.
Variables can be changed throughout your program, so you can update the information they hold as needed.
To declare a variable in JavaScript, you need to use the let
keyword, followed by the name you want to give to your variable. To assign a value to your variable, you can use the equal sign =
, followed by the value you want to store.
For example, if you want to create a variable to store someone's age, you can write:
Notice that we use semicolons ;
at the end of each statement. In JavaScript, a semicolon is used to mark the end of a statement. It tells JavaScript that you're done writing a particular instruction and you're moving on to the next one. While using semicolons is not always required in JavaScript, it's considered a best practice to include them at the end of each statement. This can help avoid errors and make your code easier to read and understand.
Now, try to declare a variable called myAge
with any number; Then, print the variable with the console.log
command.