ITEC54 - JavaScript Variable and Data Types
ITEC54 - JavaScript Variable and Data Types
A JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript : local
variable and global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers). ✓
Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign. ✓
After first letter we can use digits (0 to 9), for example value1.
✓ JavaScript variables are case sensitive, for example x and X are different variables.
<html>
<body>
<script>
var x = 10;
var y = 20;
var z = x + y;
document.write(z);
</script>
</body>
</html>
For example:
<script>
function myEx() {
var x = 10; //local variable
}
</script>
Or,
<script>
If(10 < 13) {
var y = 20; //JavaScript local variable
}
</script>
Example 1:
<html>
<body>
<script>
var data = 200; //global variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a(); //calling JavaScript function
b();
</script>
</body>
</html>
Example 2:
<html>
<body>
<script>
var value=50; //global variable
function a(){
alert(value);
}
function b(){
alert(value);
}
a(); //calling JavaScript function
</script>
</body>
</html>
Declaring JavaScript global variable within function
To declare JavaScript global variables inside function, you need to use window object. For example:
window.value = 90;
Now it can be declared inside any function and can be accessed from any function. For example:
<html>
<body>
<script>
function m(){
window.value = 100; //declaring global variable by window object
}
function n(){
alert(window.value); //accessing global variable from other function
}
m();
n();
</script>
</body>
</html>
<html>
<body>
<script>
var value = 50;
function a(){
alert(window.value); //accessing global variable
}
a(); //calling JavaScript function
</script>
</body>
</html>
When adding a number and a string, JavaScript will treat the number as a string.
Example: Result:
<html>
<body>
<h2>JavaScript</h2>
<p id="sample"></p>
<script>
var x = 16 + "Volvo";
document.getElementById("sample").innerHTML = x;
</script>
</body>
</html>
Example: Result:
<html>
<body>
<h2>JavaScript</h2>
<p id="sample"></p>
<script>
var x = "Volvo" + 16;
document.getElementById("sample").innerHTML = x;
</script>
</body>
</html>
Note: JavaScript evaluates expressions from left to right. Different sequences can produce different results:
Example 1: Result:
<html>
<body>
<h2>JavaScript</h2>
<p id="sample"></p>
<script>
var x = 16 + 4 + "Volvo";
document.getElementById("sample").innerHTML = x;
</script>
</body>
</html>
Example 2: Result:
<html>
<body>
<h2>JavaScript</h2>
<p id="sample"></p>
<script>
var x = "Volvo" + 16 + 4;
document.getElementById("sample").innerHTML = x;
</script>
</body>
</html>
In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo". In the
second example, since the first operand is a string, all operands are treated as strings.
JavaScript Strings
A string (or a text string) is a series of characters like "John Doe".
Strings are written with quotes. You can use single or double quotes.
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
Example:
var answer1 = "It's alright"; // Single quote inside double quotes
var answer2 = "He is called 'Johnny'"; // Single quotes inside double quotes
var answer3 = 'He is called "Johnny"'; // Double quotes inside single quotes
JavaScript provides different data types to hold different types of values. There are two types of data types in
JavaScript.
String: Result:
<html>
<body>
<h2>JavaScript Strings</h2>
<p id="sample"></p>
<script>
var carName1 = "Juan";
var carName2 = 'Dela Cruz';
document.getElementById("sample").innerHTML =
carName1 + "<br>" +
carName2;
</script>
</body>
</html>
Number: Result:
<html>
<body>
<h2>JavaScript Numbers</h2>
<p id="sample"></p>
<script>
var x1 = 34.00;
var x2 = 34;
var x3 = 3.14;
document.getElementById("sample").innerHTML =
x1 + "<br>" + x2 + "<br>" + x3;
</script>
</body>
</html>
Boolean: Result:
<html>
<body>
<h2>JavaScript Booleans</h2>
<p id="sample"></p>
<script>
var x = 5;
var y = 5;
var z = 6;
document.getElementById("sample").innerHTML =
(x == y) + "<br>" + (x == z);
</script>
</body>
</html>
Undefined: Result:
<html>
<body>
<h2>JavaScript Undefined</h2>
<p id="demo"></p>
<script>
var car;
document.getElementById("demo").innerHTML =
car + "<br>" + typeof car;
</script>
</body>
</html>
Null: Result:
<html>
<body>
<h2>JavaScript</h2>
<p id="sample"></p>
<script>
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;
document.getElementById("sample").innerHTML = typeof person;
</script>
</body>
</html>
The “typeof” operator
<html>
<body>
<h2>JavaScript typeof</h2>
<p>The typeof operator returns the type of a variable or an
expression.</p> <p id="sample"></p>
<script>
document.getElementById("sample").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof true + "<br>" +
typeof false + "<br>" +
typeof x;
</script>
</body>
</html> Result:
Object: Result:
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="sample"></p>
<script>
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("sample").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
</body>
</html>
Array: Result:
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="sample"></p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("sample").innerHTML = cars[0];
</script>
</body>
</html>
JavaScript Operators
JavaScript operators are symbols that are used to perform operations on operands. For example:
var sum = 10 + 20;
Types of Operators
There are following types of operators in JavaScript.
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
Example:
<html>
<body>
<script>
var a = 10;
var b = 20;
var linebreak = "<br />";
Example:
<html>
<body>
<script>
var a = 2; // Bit presentation 10
var b = 3; // Bit presentation 11
var linebreak = "<br />";
Example:
<html>
<body>
<script>
var a = true;
var b = false;
var linebreak = "<br />";
Example:
<html>
<body>
<script>
var a = 33;
var b = 10;
var linebreak = "<br />";
Operator Description
(?:) Conditional Operator returns value based on the condition. It is like if-else. , Comma
Operator allows multiple expressions to be evaluated as single statement. delete Delete
Operator deletes a property from the object.
in In Operator checks if object has the given property
instanceof checks if the object is an instance of given type
new creates an instance (object)
typeof checks the type of object.
void it discards the expression's return value.
yield checks what is returned in a generator by the generator's iterator.
JavaScript If statement
It evaluates the content only if expression is true. The signature of JavaScript if statement is given below.
if(expression){
//content to be evaluated
}
Example:
<html>
<body>
<script>
var age = 20; //Set the variable to different value and then try
if( age > 18 ) {
document.write("<b>Qualifies for driving</b>");
}
</script>
</body>
</html>
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false
}
Example:
<html>
<body>
<script>
var age = 15; // Set the variable to different value and then try
if( age > 18 ){
document.write("<b>Qualifies for driving</b>");
}
else{
document.write("<b>Does not qualify for driving</b>");
}
</script>
</body>
</html>
if(expression1){
//content to be evaluated if expression1 is true
}
else if(expression2){
//content to be evaluated if expression2 is true
}
else if(expression3){
//content to be evaluated if expression3 is true
}
else{
//content to be evaluated if no expression is true
}
Example:
<html>
<body>
<script>
var book = "maths"; //Set the variable to different value and then try if( book
== "history" ) {
document.write("<b>History Book</b>");
}
else {
document.write("<b>Unknown Book</b>");
}
</script>
</body>
<html>