Declarations
There are three kinds of declarations in JS.
var
declares a variable.let
declares a variable that is block-scoped (more on this later).const
declares 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-9
may follow any of these charactersh3ll0
.JavaScript is a case-sensitive language:
HELLO
andHeLLo
are two different variables.
Declaring Variables
There are three ways to declare a variable in JavaScript:
The
var
keyword:var newVariable;
Assign a variable a value:
newVariable = 12
The
let
orconst
keywords:let newVariable = 12
;const newVariable = 12
File Location
Practice
In
declarations.js
, use thevar
keyword 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