JS
JS
It was introduced in the year 1995 for adding programs to the webpages in the
Netscape Navigator browser.
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="js">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("js").innerHTML = "Hello
JavaScript!"'>Click Me!</button>
</body>
</html>
<html>
<body>
<h2>Welcome to JavaScript</h2>
<script>
document.write("Hello JavaScript");
</script>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="message.js"></script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="dis()"/>
</form>
</body>
</html>
function dis(){
alert("Hello Javascript");
}
<script>
// It is single line comment
document.write("hello javascript");
</script>
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comment");
</script>
JavaScript Variable
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).
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
<script>
function abc(){
var x=10;//local variable
}
</script>
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
JavaScript Let
The let keyword was introduced in ES6 (2015).
let x = "xyz";
let x = 0;
// SyntaxError: 'x' has already been declared
JavaScript is a dynamic type language, means we don't need to specify type of the
variable because it is dynamically used by JavaScript engine. We need to use var
here to specify the data type. It can hold any type of values such as numbers,
strings etc.
JavaScript Operators
JavaScript If-else
The JavaScript if-else statement is used to execute the code whether condition is
true or false. There are three forms of if statement in JavaScript.
If Statement
if(expression){
//content to be evaluated
}
<html>
<body>
<script>
var marks=90;
if(a>80){
document.write("Congrats");
}
</script>
</body>
</html>
If else statement
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false
}
<script>
var a=17;
if(a%2==0){
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>
if else if statement
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
}
JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple
expressions.
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
......
default:
code to be executed if above values are not matched;
}
<script>
var grade='B';
var result;
switch(grade){
case 'A':
result="A Grade";
break;
case 'B':
result="B Grade";
break;
case 'C':
result="C Grade";
break;
default:
result="No Grade";
}
document.write(result);
</script>
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do
while or for-in loops.
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
do{
code to be executed
}while (condition);
<script>
var i=1;
do{
document.write(i + "<br/>");
i++;
}while (i<=5);
</script>
JavaScript Functions
JavaScript functions are used to perform operations, can call JavaScript function
many times to reuse the code.
<script>
function msg(){
alert("hello! this is js");
}
</script>
<input type="button" onclick="msg()" value="call function"/>
<script>
function getcube(number){
alert(number*number*number);
}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
<script>
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
</script>
<script>
var pow=new Function("num1","num2","return Math.pow(num1,num2)");
document.writeln(pow(2,3));
</script>