VARIABLES & DATA TYPES IN
JAVASCRIPT
Variables
• JavaScript application work with information.
• examples:
An online shop –goods being sold, a shopping
cart.
A chat application –users, messages, and
much more.
Variables are used to store this information.
• A variable is a “named storage” for data.
• We can use variables to store goods, visitors, and
other data.
• use the let keyword.
The statement below creates (in other
words: declares) a variable with the name
“message”:
let message;
Now, we can put some data into it by using the
assignment operator =:
let message; message = 'Hello'; // store the string
The string is now saved into the memory area associated
with the variable. We can access it using the variable
name:
let message;
message = 'Hello!';
alert(message); // shows the variable content
To be concise, we can combine the variable declaration
and assignment into a single line:
let message = 'Hello!'; // define the variable and assign the
value alert(message); // Hello!
We can also declare multiple variables in one line:
let user = 'John', age = 25, message = 'Hello';
let user = 'John';
let age = 25;
let message = 'Hello';
let user = 'John', age = 25, message = 'Hello';
…Or even in the “comma-first” style:
let user = 'John' , age = 25 , message = 'Hello';
assume the box of message
let message;
message = 'Hello!';
message = 'World!'; // value changed
alert(message);
Note:-When the value is changed, the old data
is removed from the variable:
We can also declare two variables and copy data
from one into the other.
let hello = 'Hello world!';
let message;
// copy 'Hello world' from hello into message
message = hello;
// now two variables hold the same data
alert(hello);
// Hello world!
alert(message);
// Hello world!
A variable should be declared only once.
A repeated declaration of the same variable is an error:
let message = "This";
// repeated 'let' leads to an error let
message = "That";
// SyntaxError: 'message' has already been declared
Reserved names
There is a list of reserved words, which cannot be used as
variable names because they are used by the language
itself.
For example: let, class, return, and function are reserved.
let let = 5;
// can't name a variable "let", error!
let return = 5;
// also can't name it "return", error!
An assignment without use strict
num = 5;
// the variable "num" is created if it didn't exist
alert(num); // 5
This is a bad practice and would cause an error in strict
mode:
"use strict";
num = 5;
// error: num is not defined
CONSTANTS
To declare a constant (unchanging) variable, use const instead of let:
const myBirthday = '18.04.1982';
•Variables declared using const are called “constants”.
• They cannot be reassigned.
•An attempt to do so would cause an error:
const myBirthday = '18.04.1982';
myBirthday = '01.01.2001';
// error, can't reassign the constant!
When a programmer is sure that a variable will never change, they can
declare it with const to guarantee and clearly communicate that fact to
everyone.
Upper constants
There is a widespread practice to use constants as aliases for difficult-
to-remember values that are known prior to execution.
Such constants are named using capital letters and underscores.
Eg:-
const COLOR_RED = "#F00";
const COLOR_GREEN = "#0F0";
const COLOR_BLUE = "#00F";
const COLOR_ORANGE = "#FF7F00";
// ...when we need to pick a color
let color = COLOR_ORANGE;
alert(color);
Output:-
// #FF7F00
Summary
Dat a can be stored by using the var, let, or const keywords.
•let – is a modern variable declaration.
•var – is an old-school variable declaration.
•const – is like let, but the value of the variable can’t be
changed.
Note:- Variables should be named in a way that allows us to
easily understand what’s inside them.
PROGRAM 1
• Declare two variables: TYBCA and SYBCA
• Assign the value “Anjali" to SYBCA
• Copy the value from SYBCA to TYBCA
• Show the value of TYBCA using alert
TASK
• Create a variable with the name of our planet.
How would you name such a variable?
• Create a variable to store the name of a
current visitor to a website. How would you
name that variable?