One Pager Cheat Sheet
- This lesson explains how to use
variables
and theirassignment
to store data in a programming language. - A variable
a
can be assigned a data value using theassignment operator
equal sign
(=), and for the duration of the program, it will correspond to that value unless reassigned. - Variables in JavaScript can be declared with the
var
keyword, and stored in memory with a value which can be displayed withconsole.log()
. Additionally,let
andconst
are also used to declare variables in order to avoidglobal variable
modification. - Using
let
andconst
instead ofvar
is a good way to prevent confusing variables in larger codebases because they are block-scoped and can only be used within specific{
and}
symbols. - Carefully following rules such as variable names only containing alphabetical letters, digits or underscore symbols, being aware of case-sensitivity and not using language-specific keywords will help to create variables correctly and avoid errors.
- No, this code snippet is not valid for declaring a variable in JavaScript, as the syntax
var <variable name>
(for example,var myVariable
) is not followed. - No valid JavaScript variable name can contain characters, so
var words: "Hello World!"
is invalid, whereas_name
is valid. - Variables can be reassigned in JavaScript by assigning a new value without using
var
, resulting in the change of their value. - Different types of variables can be used to perform different kinds of operations, such as mathematical operations for numbers and concatenation for strings.
- The value of
a
has been changed from "Hello" to "World". - The code
concatenates
thestrings
stored infirstName
andlastName
in to a newvariable
name
, which is thenconsole.log
ged to outputAnnaGreen
. - Creating variables with meaningful names is an important step in the programming process to avoid
confusion
of which variables is used to store what value.