JavaScript
Dr. Waheed Anwar
Computer Science
• Outline
➢ Importance of JavaScript
➢ Runtime
➢ new features
➢ Variables with var, let and const
➢ identifiers
➢ datatypes
Computer Science
JavaScript in Webpages
Computer Science
What JavaScript Can do/ Can’t do
Computer Science
JavaScript Runtime
Computer Science
ES6 Standard
European Computer Manufacturers Association(ECMA)
Official Website for JavaScript ES6: https://tc39.es/ecma262
Computer Science
JavaScript ES6 New Features
Computer Science
Variables in JavaScript
Variables are Containers for Storing Data
JavaScript Variables can be declared in 4 ways:
1. Automatically
2. Using var
3. Using let
4. Using const
When to Use var, let, or const?
1. Always declare variables
2. Always use const if the value should not be changed
3. Always use const if the type should not be changed (Arrays and Objects)
4. Only use let if you can't use const
5. Only use var if you MUST support old browsers.
Computer Science
Const vs Let
const
1. The const keyword was introduced in ES6 (2015)
2. Variables defined with const cannot be Redeclared
3. Variables defined with const cannot be Reassigned
4. Variables defined with const have Block Scope
let
1. The let keyword was introduced in ES6 (2015)
2. Variables declared with let have Block Scope
3. Variables declared with let must be Declared before use
4. Variables declared with let cannot be Redeclared in the
same scope
Computer Science
Key words let and const
Computer Science
Identifiers in JavaScript
o All JavaScript variables must be identified with unique
names.
o These unique names are called identifiers.
o Identifiers can be short names (like x and y) or more
descriptive names (age, sum, totalVolume).
The general rules for constructing names for variables (unique
identifiers) are:
1. Names can contain letters, digits, underscores, and dollar
signs.
2. Names must begin with a letter.
3. Names can also begin with $ and _
4. Names are case sensitive (y and Y are different variables).
5. Reserved words (like JavaScript keywords) cannot be used
as names.
Computer Science
Re-Declaring JavaScript Variables
If you re-declare a JavaScript variable declared with var, it
will not lose its value.
var pName = “laptop T470”;
var pName;
The variable pName will still have the value “laptop T470”
after the execution of these statements:
Note
You cannot re-declare a variable declared
with let or const.
This will not work:
let pName = “laptop T470”;;
let pName;
Computer Science
Data Tyes in JavaScript
Computer Science
Variable Declaration Rules in JavaScript
Computer Science
JavaScript String
Computer Science
JavaScript Number
Computer Science
JavaScript Boolean
Computer Science
JavaScript Undefined
Computer Science
JavaScript null
Computer Science
JavaScript Object
Computer Science