JavaScript Variables
4 Ways to Declare a JavaScript Variable:
- Using
var - Using
let - Using
const - Using nothing
What are Variables?
Variables are containers for storing data (storing data values).
In this example,
x,y, andz, are variables, declared with thevarkeyword:Redeclaring a Variable Using let
Redeclaring a variable inside a block will not redeclare the variable outside the block:
Redeclaring a variable using the
letkeyword can solve this problem.When to use JavaScript const?
Always declare a variable with
constwhen you know that the value should not be changed.Use
constwhen you declare:- A new Array
- A new Object
- A new Function
- A new RegExp
Constant Objects and Arrays
The keyword
constis a little misleading.It does not define a constant value. It defines a constant reference to a value.
Because of this you can NOT:
- Reassign a constant value
- Reassign a constant array
- Reassign a constant object
- Change the elements of constant array
- Change the properties of constant object
But you CAN:
Constant Arrays
You can change the elements of a constant array:
No comments:
Post a Comment