Page 127 - CITS - Computer Software Application -TT
P. 127
COMPUTER SOFTWARE APPLICATION - CITS
Strings are written inside double or single quotes. Numbers are written without quotes.
If you put a number in quotes, it will be treated as a text string.
Example
const pi = 3.14;
let person = “John Doe”;
let answer = ‘Yes I am!’;
Declaring a JavaScript Variable
Creating a variable in JavaScript is called “declaring” a variable.
You declare a JavaScript variable with the var or the let keyword:
var carName;
or:
let carName;
After the declaration, the variable has no value (technically it is undefined).
To assign a value to the variable, use the equal sign:
carName = “Volvo”;
You can also assign a value to the variable when you declare it:
let carName = “Volvo”;
In the example below, we create a variable called carName and assign the value “Volvo” to it.
Then we “output” the value inside an HTML paragraph with id=”demo”:
Example
<p id=”demo”></p>
<script>
let carName = “Volvo”;
document.getElementById(“demo”).innerHTML = carName;
</script>
Note
It’s a good programming practice to declare all variables at the beginning of a script.
One Statement, Many Variables
You can declare many variables in one statement.
Start the statement with let and separate the variables by comma:
Example
let person = “John Doe”, carName = “Volvo”, price = 200;
A declaration can span multiple lines:
Example
let person = “John Doe”,
carName = “Volvo”,
price = 200;
114
CITS : IT&ITES - Computer Software Application - Lesson 37 - 46