JavaScript
Java Script Variables
A variable is a location where data can be stored. Local variables and global variables are the two types of variables. Local variables are declared inside block or function and are available only within those blocks or functions. A global variable is declared outside the function or with window object and is available from any function. Window object is used when a global variable has to be declared inside a function. A global variable declared outside a function can be accessed by window object.
Rules for declaring variables
• Variables must have unique names (identifiers)
• Variable names must begin with a letter, underscore (_) or a dollar sign ($)
• Variable names are case sensitive
• Names can contain letters, digits, dollar signs or underscore
• Reserved words cannot be used as names
Variables can be declared with or without using the keywords.
Keywords to declare the variables
• Var
• Let
• const
Examples of declaration of variables
• var x=1
• let x=1
• const x=1
• x=1
Key Points to note
• Variable declared with “var” do not have block scope
• If “let” is used the value of the variable can change
Comment in JavaScript is denoted by //.
//This is a comment line
Multiple lines can be commented using /* */
/*
This is comment line1
This is comment line2
*/
Example1: Add two numbers
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Variables</title>
</head>
<body>
<script>
var x = 10, y = 20
var z = x + y
document.write(z)
alert(z) //this is a message box
</script>
</body>
</html>
output: 30
Example2: When string is added to number they are concatenated
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Variables</title>
</head>
<body>
<script>
var s = “10” + 20
document.write(s)
</script>
</body>
</html>
Output:1020
Example3: Variables declared with “var” do not have block scope, that means variable inside block { } can be accessed from outside
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Variables</title>
</head>
<body>
<script>
var x = 10
document.write(x + “<br>”)
{
var x = 20
document.write(x + “<br>”)
}
document.write(x)
</script>
</body>
</html>
Output:
10
20
20
In the above example if “var” is changed to “let”, the output will be 10,20,10. In this case variable inside the block { } cannot be accessed from outside
Example4: Variable declared with “const” keyword cannot be reassigned or redeclared
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Variables</title>
</head>
<body>
<script>
const x = 10
document.write(x + “<br>”)
x = 20 //cannot reassign
document.write(x + “<br>”)
</script>
</body>
</html>
Output: 10
Example5: Using “const” keyword with array
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Variables</title>
</head>
<body>
<script>
const arr = [‘a’, ‘d’, ‘c’]
document.write(arr+”<br>”)
arr[1] = ‘b’
document.write(arr + “<br>”)
arr.push(‘d’)
document.write(arr + “<br>”)
</script>
</body>
</html>
output:
a,d,c
a,b,c
a,b,c,d