Module 8.0 - Lesson 8.3 Vriable Declaration and Data Type (Primitive)
Module 8.0 - Lesson 8.3 Vriable Declaration and Data Type (Primitive)
CODE
OUTPUT
Statement declaring a variable (x),
using a single equal sign, assigns a
number data type (5) then
followed by a semicolon (;). A
variable can change as it was
used the first time and was
declared with var.
When we use the let keyword rather than var; it imposes scope constraints. The
let declared variables cannot be redeclared. This will give you a SyntaxError if
you will try to do so.
The scope of those variables is the block they are defined in, also in any sub-
blocks.
The browser will throw a ReferenceError if you are trying to access these
variables outside of their block
USING CONST KEYWORD
Declaring your constants in uppercase is a good practice, because this may help
programmers differentiate the constants from other variables within the program.
Much like variables defined using the let statement, constants are block-scoped. The const
declaration creates a constant whose scope is often either global or local to the declared
block during which we declare it. The value of a constant is read-only; you cannot change it
through reassignment, and you cannot redeclare it. An initializer for a constant is required;
i.e., you want to specify its value within the same statement in which it is declared (since
you cannot alter it later). A function or a variable within the same scope cannot have the
same name as the variable.
block-scoped - variable is accessible within the block that is between the curly braces.
global - visible and available to all statements in a setup script that follow its declaration.
local - visible and available only within the function where they are declared.
HOW TO DECLARE VARIABLES IN JAVASCRIPT?
In JavaScript, we can declare in 3 different ways:
It is best when we understand the var, let and const with three concepts:
a. Scope
b. Reassigning a new value
c. When you access a variable before declaring it
VARIABLE SCOPE IN JAVASCRIPT
In JavaScript, we use scope as a way to identify where and whether we can use a
variable. The variables may exist within a block, inside a function, or outside a function
and block.
CONCLUSION: Do not use the var keyword inside a block (block scope). Always use let and
const instead.
HOW TO USE JAVASCRIPT VARIABLES IN FUNCTIONAL SCOPE
In functional Scope, a variable declared inside a function using these keywords is not
accessible outside the function.
MINDM AP OF THE SE THRE E K E YW OR D S W I T H R E F E R E NCE
TO DI FFER ENT SC OPE S
MINDM AP TO HE LP YOU G RA SP H OW R E ASSI GNI NG W OR KS
F OR V AR I AB LES D E C LA RE D WITH T HE SE T HR E E KE YW OR D S
MINDMAP AGAIN TO HELP YOU UNDERSTAND IT VISUALLY. IN
THE MINDMAP, VAR IS DEPICTED FOR NON-STRICT MODE.
STATEM ENTS/SCRIPT
Each line in JavaScript is an instruction or a script. When the browser reads it,
it executes the script
VARI AB LES
Declare a variable
Variables hold content
(give it a name)
Words, numbers, true/false,
basically any kind of content.
Initialize variable
(give it a value)
VARI AB LES
Declare a variable
Declare and initialize at the (give it a name)
same time.
Initialize variable
(give it a value)
DATA TY PES
NAMI NG R ULES