2.2 Variables in JavaScript
2.2 Variables in JavaScript
Agenda
JavaScript Variables
Rules to be followed While Declaring a Variable
Syntax (Correct & Incorrect)
Valid and Invalid Declaration
Initializing a Variable
Javascript Dynamic Typing
JavaScript Variable Scope
JavaScript Data Types
JavaScript Arithmetic
When to Use var, let, or const?
Variables in JavaScript
3 31-10-2023
Variables in JavaScript
4 31-10-2023
Course Outcomes:
CO. Outcome K Level
(Apply) Understand and apply JavaScript concepts for dynamic web page
CO2 K3
design
CO3 (Apply) Understand and apply shell commands and GIT workflow K2
Variables in JavaScript
5 31-10-2023
Challenge time:
➢ Different ways to include JS in HTML
➢ Tag to include JS in HTML
➢ How to display alert message using JS?
Variables in JavaScript
6 31-10-2023
JavaScript Variables:
➢ Variable is a name given to a memory location that is used to
store any type of data.
➢ As the name suggests, variables mean it can vary, i.e., data
stored in a variable can be changed or updated later in the
program if required.
➢ Highlight: A Javascript variable is a container, not a value; this
means that variables aren’t themselves values; they are a
container for values.
Variables in JavaScript
7 31-10-2023
Variables in JavaScript
9 31-10-2023
Example Declaration:
➢ Declaring a JavaScript variable using line breaks and spaces:
var a = 25;
➢ Declaring two variables in a single line separated by a comma:
var b=1, c=2;
➢ Using reserve keywords while declaring a variable:
var return = 5;
//Will give an error, as the return is a keyword
//in Javascript
Variables in JavaScript
11 31-10-2023
Variables in JavaScript
12 31-10-2023
Initializing a Variable:
➢ After you have declared a variable, you can initialise that
variable with a value.
➢ This initialisation can be done by just writing the variable name
followed by an equals sign and then the value you want the
variable to store.
//declaring and initializing at the same time
var age = 20;
schoolName = 'XYZ School’;
//address stores "abc"
address = 'abc’; //Global Variable
Variables in JavaScript
13 31-10-2023
Variables in JavaScript
14 31-10-2023
Variables in JavaScript
15 31-10-2023
const pi = 3.14;
let person = “Karthi";
let answer = 'Yes I am!';
Variables in JavaScript
16 31-10-2023
JavaScript Arithmetic
➢ let x = 5 + 2 + 3;
➢ let x = “Karthick" + " " + “Raja";
➢ let x = "5" + 2 + 3; //Any Guess
➢ let x = 2 + 3 + "5";
Variables in JavaScript
17 31-10-2023
Variables in JavaScript
18 31-10-2023
Summary:
➢ Variables are containers that store values.
➢ Variables can be declared using var and let keywords.
➢ Variables can be updated or modified anytime and anywhere
in its scope.
➢ We cannot use any javascript reserved keywords as a variable
name.
➢ In JavaScript, there are only two types of variable scopes:
➢ Global scope
➢ Local scope
➢ Constants in JavaScript are those values that do not change
throughout the code.
Variables in JavaScript
19 31-10-2023
Variables in JavaScript
21 31-10-2023
Variables in JavaScript