HTML
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Variables : let, var & const</title>
</head>
<body>
<!--
Variables are containers for storing values.
Four Ways to declare a JavaScript Variable:
- Using var
- Using let
- Using const
- Using nothing
General rules for constructing names for variables:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter.
- Names can also begin with $ (dollar) and _ (underscores)
- Names are case-sensitive
- Reserved words cannot be used as names.
Creating a variable in JavaScript is called "declaring" a variable.
Variable declared without a value will have, value undefined.
Cannot re-declare a variable declared with let or const.
-->
<h1>Demo: Variables : let, var & const</h1>
<!-- JavaScript code Starts here -->
<script>
/************************************************/
// JavaScript Variable Example
// Declare a JavaScript Variable using keywords var, let, const
//
var num1 = 5; // Variable name can contain numbers like num1
const num2 = 6;
let sum = num1 + num2; // variable "sum" will store value of 11 = ( 5 + 6 )
document.write("The value of sum is: " + sum + "<BR><BR>")
/************************************************/
// JavaScript Variable Example
// Variable names can start with "$" symbol like in $price1
//
const $price1 = 5;
const $price2 = 5;
let total = $price1 + $price2;
document.write("The total is: " + total + "<BR><BR>")
/************************************************/
// JavaScript Variable Example
// Variable names can start with "_" (underscore) symbol like in _name
//
let _name = "FIFA ";
_name += "World Cup 2022";
document.write(_name + "<BR><BR>")
/************************************************/
// JavaScript Variable Example
// variable declared without a value will have the value undefined
//
let salary;
document.write("The salary amount is : " + salary + "<BR><BR>")
/************************************************/
// JavaScript Variable Example
// If you put a number in quotes, the rest of the numbers will be treated as strings, and concatenated
//
const intNum = 3;
const str = "31"; // Numbers within quotes will be treated as strings
let sumUp = str + intNum; // concatenation of number and string
document.write("Result (sumUp) is : " + sumUp + " and the typeof is : " +typeof(sumUp))
</script>
<!-- JavaScript code Ends here -->
</body>
</html>