What is JavaScript ?
JavaScript is the world's most popular programming language.
JavaScript is the programming language of the Web.
JavaScript to program the behavior of web pages.
JavaScript Where To write
Internal js:
JavaScript code is inserted between
<script>
and</script>
tags. JavaScript in<head>
orjs in head
<!DOCTYPE html> <html> <head> <script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } </script> </head> <body> <h2>Demo JavaScript in Head</h2> <p id="demo">A Paragraph</p> <button type="button" onclick="myFunction()">Try it</button> </body> </html>
js in body
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Demo JavaScript in Head</h2>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
External js:
- create a file with js extension and link the html file.
like:-
<script src="scriptfile.js"> </script>
Variables:-
Variable is ued to store the data, such as container
Variable a name of storage location.
variable are three types.
let
syntax:
Data Types
A JavaScript variable can hold any type of data.
JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
Primitive data type
Non-primitive data type
// Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let x = true;
let y = false;
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
Operators
JavaScript operators are symbols that are used to perform operations on operands. For example:
var sum=10+20;
Here, + is the arithmetic operator and = is the assignment operator.
here are following types of operators in JavaScript.
Arithmetic Operators:-
Comparison Operators:-
Bitwise Operators:-
Logical Operators:-
Assignment Operators:-
JavaScript If-else
If Statement
If else statement
if else if statement
If statement:-
<script>
var a=20;
if(a>10){
document.write("value of a is greater than 10");
}
</script>
If...else Statement:-
<script>
var a=20;
if(a%2==0){
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>
If...else if statement:-
<script>
var a=20;
if(a==10){
document.write("a is equal to 10");
}
else if(a==15){
document.write("a is equal to 15");
}
else if(a==20){
document.write("a is equal to 20");
}
else{
document.write("a is not equal to 10, 15 or 20");
}
</script>
JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple expressions. It is just like else if statement.
<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.It makes the code compact. It is mostly used in array.
for loop
while loop
do-while loop
## For loop:-
The JavaScript for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known.
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
while loop:-
The JavaScript while loop iterates the elements for the infinite number of times. It should be used if number of iteration is not known.
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
do while loop:-
The JavaScript do while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false.
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>