0% found this document useful (0 votes)
23 views

Module 8.0 - Lesson 8.3 Vriable Declaration and Data Type (Primitive)

This document discusses JavaScript variable declarations, data types, and naming rules. It explains the differences between using var, let, and const to declare variables and how they affect variable scoping and reassignment. The document also provides examples of declaring, initializing, and assigning values to variables in JavaScript.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Module 8.0 - Lesson 8.3 Vriable Declaration and Data Type (Primitive)

This document discusses JavaScript variable declarations, data types, and naming rules. It explains the differences between using var, let, and const to declare variables and how they affect variable scoping and reassignment. The document also provides examples of declaring, initializing, and assigning values to variables in JavaScript.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

VARIABLE DECLARATIONS

AND DATA TYPES


(PRIMITIVE)
MODULE 8_LESSON 8.3
JAVASCRIPT VARIABLES

VARIABLES are also known as holders or


containers. These containers will allow you
There is no value or meaning to x but a
to position data and then basically refer
value can be added to it. Presently, x
to the data by naming the container.
reflects 5. Consider x, which is a number,
to be a container that stores 5.
Variables can be known as something
that may change. A number defined by a In JavaScript, variables work the same,
letter is called a variable in basic but they will be worth more than just a
algebra. X might be a common name for number. To declare a new variable in
JavaScript we can use the keywords; var,
a variable, but Y, Z, or another name may
let and const.
describe it just as easily

So what are their differences???


USING VAR KEYWORD

CODE

var keyword is used to create and


declare a variable. To construct a
JavaScript statement, you can refer
to our algebra. It is possible to
reassign or redefine the values of
the variables.

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.

var x = 5; // x was worth 5


x = 20; // now it’s worth 20

The variable holds one value at a


time, and because the program
runs from top to bottom, x has now
a value of 20.
Multiple variables can be declared in a single line,
separating them with commas.

var x = 5, var greeting = "Oh hi Pisay",…; or


variables could also be defined like below:
var x=y=z=5;
wherein x, y and z will all have 5 as their initial value.
IMPORTANT NOTES:

Before program execution, all the declarations in JavaScript (including


variable declarations) are processed. In this way, declaring a variable
anywhere is equivalent to declaring it at the top of the code. This
provides JavaScript with the functionality to use the variables before
their declarations within the code. We call this behavior of variables “var
hoisting”.
It is always recommended in programming to declare the
variables before using them. This is to avoid unintentional
redeclaration of a variable.

This type of variable declaration method is never used by


programmers. Other two methods you need to consider are
either let or const for your variables.
USING LET KEYWORD

let variables are declared in a similar manner to var declared variables.

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.

So, what is a block? A block (that is, a


code block) is a section of the code we
define using a pair of curly brace
s({...}).

Anything and everything outside of a block or a


function we'll call Global. So, when we declare
variables, they can exist within a block, inside a
function, or outside of a block/function – that is,
they have global scope.
HOW TO USE JAVASCRIPT VARIABLES IN BLOCK SCOPE
If we do not want a
variable declared inside a
{ } block to be accessed
outside of the block, you
need to declare them
using the let or const
keywords. Variables
declared with the var
keyword inside the { }
block are accessible
outside of the block too.
On the example above, the value of the age variable may get
overridden unknowingly and eventually introduce a bug.

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

None of the variables are


accessible outside of the function,
not even age which is declared
using var.

CONCLUSION: Variable declared


with var inside a function is not
accessible outside of it. The
keyword var has function-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

string - a group of characters in quotes (" ", ' ')

number- a number (numerical)

boolean - yes or no (true or false)


DATA TY PES

In nerd speak, JavaScript undefined - no value yet.


variables are loosely typed.
You don't know the kind of null- a purposely empty value
value a variable will have until (not the same as 0).
you assign it.

NAMI NG R ULES

Begin with a letter, _, or $


Names are case sensitive
Contain letters, numbers,
_ and $
QUESTIONS
???
APPLY WHAT
YOU HAVE LEARN
Develop a JavaScript program that will measure how
much your favorite snack is for a lifetime supply.

Declare a variable to store your present age.


Declare a variable with a maximum age (maxAge) that will be processed.
Declare a variable that stores an estimated daily amount (numPerDay) consumed of
your favorite snack
Calculate the average amount you will consume for the rest of your life.
totalRequired = (numPerDay * 365) * (maxAge - age)
The result should be shown on the screen as follows: "You will need NN to last you until
the ripe old age of X".

You might also like