Page 126 - CITS - Computer Software Application -TT
P. 126
COMPUTER SOFTWARE APPLICATION - CITS
Mixed Example
const price1 = 5;
const price2 = 6;
let total = price1 + price2;
The two variables price1 and price2 are declared with the const keyword.
These are constant values and cannot be changed.
The variable total is declared with the let keyword.
The value total can be changed.
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.
Just Like Algebra
Just like in algebra, variables hold values:
let x = 5;
let y = 6;
Just like in algebra, variables are used in expressions:
let z = x + y;
From the example above, you can guess that the total is calculated to be 11.
Note
Variables are containers for storing values.
JavaScript Identifiers
All JavaScript variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, total Volume).
The general rules for constructing names for variables (unique identifiers) are:
• Names can contain letters, digits, underscores, and dollar signs.
• Names must begin with a letter.
• Names can also begin with $ and _ (but we will not use it in this tutorial).
• Names are case sensitive (y and Y are different variables).
• Reserved words (like JavaScript keywords) cannot be used as names.
Note
JavaScript identifiers are case-sensitive.
JavaScript Data Types
JavaScript variables can hold numbers like 100 and text values like “John Doe”.
In programming, text values are called text strings.
JavaScript can handle many types of data, but for now, just think of numbers and strings.
113
CITS : IT&ITES - Computer Software Application - Lesson 37 - 46