Declarations
There are three kinds of declarations in JS.
vardeclares a variable.letdeclares a variable that is block-scoped (more on this later).constdeclares a block-scoped variable, that is a constant.
Variables
A variable is a container of memory that holds a value. Declaring a variable is as simple as writing var myVariable;. A variable can be declared in this way, but does not have a value until it is initialized by assigning it one: myVariable = 0;. In JavaScript we can declare a variable and initialize it at the same time: var myVariable = 0;. It can contain virtually anything, but it has certain naming conventions that must be followed:
A variable MUST begin with a letter
a-z, an underscore_, or a dollar sign$.Numbers
0-9may follow any of these charactersh3ll0.JavaScript is a case-sensitive language:
HELLOandHeLLoare two different variables.
Declaring Variables
There are three ways to declare a variable in JavaScript:
The
varkeyword:var newVariable;Assign a variable a value:
newVariable = 12The
letorconstkeywords:let newVariable = 12;const newVariable = 12
File Location
javascript-library
└── 0-PreWork
└── 1-Fundamentals
└── 1-Grammar-and-Types
01-comments.js
02-declarations.js <-- You are herePractice
In
declarations.js, use thevarkeyword to create two variables and assign each a value.Print both variables to the console.
Change the values in each variable, then print both to the console again.
Last updated