Page 191 - Web_Application_v2.0_C12_Fb
P. 191
Concatenation of Variables
The + operator serves two purposes in JavaScript: it can concatenate strings and variables or add numbers.
If both operands are numbers, it performs addition. However, when one or both operands are strings, it
combines them into a single string. The following examples demonstrate how the + operator works in
different scenarios:
var itemPrice = "The cost of a notebook is " + 25;
var ageInfo = "My brother is " 18 + " years old.";
If both the operands are numeric, then the + sign performs an addition operation.
Example 3: To calculate the sum of two numbers using JavaScript
<!DOCTYPE HTML>
<HTML>
<BODY>
<SCRIPT>
var num1 = 50;
var num2 = 20;
var total = num1 + num2;
document.write("The total is " +total);
</SCRIPT>
</BODY>
</HTML>
Output:
Declaring Constants
A constant is a type of variable whose value cannot be modified while the program is running. Constants in
JavaScript are declared using the const type, and the name of the constant is only in uppercase by convention.
The JavaScript constant variable is supported by a large number of browsers.
For example:
const PI=3.14
const RATE=8.9
Keywords
A keyword is a reserved word that has a special meaning for the JavaScript interpreter. These terms are not
allowed to be used as variable names.
JavaScript Part 2 189

