tags, JavaScript syntax like semicolons and case sensitivity, common data types like numbers and strings, variables, operators, and more. It provides examples of how to write JavaScript code and integrate it with HTML."> tags, JavaScript syntax like semicolons and case sensitivity, common data types like numbers and strings, variables, operators, and more. It provides examples of how to write JavaScript code and integrate it with HTML.">
Java Script
Java Script
Java Script
<script ...>
JavaScript code
</script>
JavaScript Syntax
<html>
<body>
<script language = "javascript" type = "text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
Case Sensitivity
JavaScript is a case-sensitive language. This means that the
language keywords, variables, function names, and any other
identifiers must always be typed with a consistent capitalization of
letters.
<body>
<input type = "button" onclick = "sayHello()" value = "Say
Hello" />
</body>
</html>
If you need a script to run as the page loads so that the script
generates content in the page, then the script goes in the <body>
portion of the document. In this case, you would not have any
function defined using JavaScript. Take a look at the following
code.
Live Demo
<html>
<head>
</head>
<body>
<script type = "text/javascript">
<!--
document.write("Hello World")
//-->
</script>
You can put your JavaScript code in <head> and <body> section
altogether as follows −
Live Demo
<html>
<head>
<script type = "text/javascript">
<!--
function sayHello() {
alert("Hello World")
}
//-->
</script>
</head>
<body>
<script type = "text/javascript">
<!--
document.write("Hello World")
//-->
</script>
<body>
.......
</body>
</html>
JavaScript Datatypes
JavaScript Variables
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators
<html>
<body>
document.write("a - b = ");
result = a - b;
document.write(result);
document.write(linebreak);
document.write("a / b = ");
result = a / b;
document.write(result);
document.write(linebreak);
document.write("a % b = ");
result = a % b;
document.write(result);
document.write(linebreak);
document.write("a + b + c = ");
result = a + b + c;
document.write(result);
document.write(linebreak);
a = ++a;
document.write("++a = ");
result = ++a;
document.write(result);
document.write(linebreak);
b = --b;
document.write("--b = ");
result = --b;
document.write(result);
document.write(linebreak);
//-->
</script>
Comparison Operators
<html>
<body>
<script type = "text/javascript">
<!--
var a = 10;
var b = 20;
var linebreak = "<br />";
document.write("(a == b) => ");
result = (a == b);
document.write(result);
document.write(linebreak);
document.write("(a < b) => ");
result = (a < b);
document.write(result);
document.write(linebreak);
document.write("(a > b) => ");
result = (a > b);
document.write(result);
document.write(linebreak);
document.write("(a != b) => ");
result = (a != b);
document.write(result);
document.write(linebreak);
document.write("(a >= b) => ");
result = (a >= b);
document.write(result);
document.write(linebreak);
document.write("(a <= b) => ");
result = (a <= b);
document.write(result);
document.write(linebreak);
//-->
</script>
Set the variables to different values and different operators
and then try...
</body>
</html>
Logical Operators
Live Demo
<html>
<body>
<script type = "text/javascript">
<!--
var a = true;
var b = false;
var linebreak = "<br />";
document.write("(a && b) => ");
result = (a && b);
document.write(result);
document.write(linebreak);
document.write("(a || b) => ");
result = (a || b);
document.write(result);
document.write(linebreak);
document.write("!(a && b) => ");
result = (!(a && b));
document.write(result);
document.write(linebreak);
//-->
</script>
<p>Set the variables to different values and different
operators and then try...</p>
</body>
</html>
The following flow chart shows how the if-else statement works.
JavaScript supports the following forms of if..else statement −
if statement
if...else statement
if...else if... statement.
<html>
<body>
<script type = "text/javascript">
<!--
var age = 20;
The most basic loop in JavaScript is the while loop which would
be discussed in this chapter. The purpose of a while loop is to
execute a statement or code block repeatedly as long as
an expression is true. Once the expression becomes false, the
loop terminates.
Flow Chart
The flow chart of while loop looks as follows −
Syntax
The syntax of while loop in JavaScript is as follows −
while (expression) {
Statement(s) to be executed if expression is true
}
<html>
<body>
document.write("Loop stopped!");
//-->
</script>
While writing a program, you may encounter a situation
where you need to perform an action over and over again. In
such situations, you would need to write loop statements to
reduce the number of lines.
The most basic loop in JavaScript is the while loop which would
be discussed in this chapter. The purpose of a while loop is to
execute a statement or code block repeatedly as long as
an expression is true. Once the expression becomes false, the
loop terminates.
Flow Chart
The flow chart of while loop looks as follows −
Syntax
The syntax of while loop in JavaScript is as follows −
while (expression) {
Statement(s) to be executed if expression is true
}
Example
Live Demo
<html>
<body>
<script type = "text/javascript">
<!--
var count = 0;
document.write("Starting Loop ");
while (count < 10) {
document.write("Current Count : " + count + "<br />");
count++;
}
document.write("Loop stopped!");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
The do...while loop is similar to the while loop except that the
condition check happens at the end of the loop. This means that
the loop will always be executed at least once, even if the
condition is false.
Flow Chart
The flow chart of a do-while loop would be as follows −
Syntax
The syntax for do-while loop in JavaScript is as follows −
do {
Statement(s) to be executed;
} while (expression);
<html>
<body>
<script type = "text/javascript">
<!--
var count = 0;
The 'for' loop is the most compact form of looping. It includes the
following three important parts −
The loop initialization where we initialize our counter to a
starting value. The initialization statement is executed before
the loop begins.
The test statement which will test if a given condition is true
or not. If the condition is true, then the code given inside the
loop will be executed, otherwise the control will come out of
the loop.
The iteration statement where you can increase or
decrease your counter.
You can put all the three parts in a single line separated by
semicolons.
Flow Chart
JavaScript - Functions
Function Definition
Calling a Function
Live Demo
<html>
<head>
<script type = "text/javascript">
function sayHello() {
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say
Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
Function Parameters
Live Demo
<html>
<head>
<script type = "text/javascript">
function sayHello(name, age) {
document.write (name + " is " + age + " years old.");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello('Zara', 7)" value =
"Say Hello">
</form>
<p>Use different parameters inside the function and then
try...</p>
</body>
</html>