In HTML, JavaScript code is inserted between <script>
and </script>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html>
Using innerHTML
:
document.getElementById(id)
method.id
attribute defines the HTML element. The innerHTML
property defines the HTML content<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
My First Web Page
My First Paragraph
11
Using document.write()
:
document.write()
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
My First Web Page
My first paragraph
11
Using window.alert()
:
window
keyword is optional<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
//OR
alert(5 + 6);
</script>
</body>
</html>
My First Web Page
My first paragraph
11
Using console.log()
:
console.log()
method in the browser to display data.<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
window.print()
method in the browser to print the content of the current window.<!DOCTYPE html>
<html>
<body>
<button onclick="window.print()">Print this page</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>In HTML, JavaScript statements are executed by the browser.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World.";
</script>
</body>
</html>
JavaScript Statements
In HTML, JavaScript statements are executed by the browser.
Hello World.
let
and const
.{
let x = 2;
}
// x can NOT be used here
var
always have Global Scope.var
keyword can NOT have block scope:var
inside a { } block can be accessed from outside the block{
var x = 2;
}
// x CAN be used here
var
var
keyword was used in all JavaScript code from 1995 to 2015.let
and const
keywords were added to JavaScript in 2015.var
can be redeclaredvar
keyword should only be used in code written for older browsers.<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = "The value of z is: " + z;
</script>
</body>
JavaScript Variables
In this example, x, y, and z are variables.
The value of z is: 11
let
let
keyword was introduced in ES6 (2015)let
have Block Scopelet
must be Declared before uselet
cannot be Redeclared in the same scope<!DOCTYPE html>
<html>
<body>
<h2>Redeclaring a Variable Using let</h2>
<p id="demo"></p>
<script>
let x = 10;
// Here x is 10
{
let x = 2;
// Here x is 2
}
// Here x is 10
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Redeclaring a Variable Using let
10
const
const
keyword was introduced in ES6 (2015)const
cannot be Redeclaredconst
cannot be Reassignedconst
have Block Scopeconst
keyword cannot be reassignedconst
when you know that the value should not be changed.const
when you declare:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p id="demo"></p>
<script>
const PI = 3.141592653589793;
PI = 3.14;
document.getElementById("demo").innerHTML = PI;
</script>
</body>
</html>
JavaScript const
3.14
<p id="demo"></p>
<script>
let a = 3;
let x = (100 + 50) * a;
document.getElementById("demo").innerHTML = x;
</script>
450
Operator | Description |
---|---|
+ | Addition |
- | Substraction |
* | Multiplication |
/ | Division |
** | Exponentiation |
% | Modulus (Remainder) |
++ | Increament |
-- | Decreament |
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
**= | x **= y | x = x ** y |
Operator | Description |
---|---|
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
? | ternary operator |
Operator | Description |
---|---|
&& | logical and |
| | logical or |
! | logical not |
typeof
operator to find the type of a JavaScript variable.typeof
operator returns the type of a variable or an expressionOperator | Description |
---|---|
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an object type |
Operator | Description | Example | Same as | Result | Decimal |
---|---|---|---|---|---|
& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
>>> | unsigned right shift | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |
<p id="demo"></p>
<script>
let carName1 = "Volvo XC60";
let carName2 = "Volvo XC90";
document.getElementById("demo").innerHTML = carName1 + "<br>" + carName2;
</script>
Volvo XC60
Volvo XC90
<p id="demo"></p>
<script>
let x1 = 34.0;
let x2 = 34;
let x3 = 3.14;
document.getElementById("demo").innerHTML = x1 + "<br>" + x2 + "<br>" + x3;
</script>
34
34
3.14
<p id="demo"></p>
<script>
let x = 5;
let y = 5;
let z = 6;
document.getElementById("demo").innerHTML = (x == y) + "<br>" + (x == z);
</script>
true
false
<p id="demo"></p>
<script>
const cars = ["Benz", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
Benz
{}
.name:value
pairs, separated by commas.<p id="demo"></p>
<script>
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue" };
}
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
John is 50 years old.
//Syntax of Function
function name(parameter1, parameter2, parameter3) {
// code to be executed
}
return
statement, the function will stop executing.<p id="demo"></p>
<script>
function myFunction(p1, p2) {
return p1 * p2;
}
let result = myFunction(4, 3);
document.getElementById("demo").innerHTML = result;
</script>
12
name:values
pairs in JavaScript objects are called propertiesconst person = {
firstName: "John",
lastName: "Doe",
id: 5566,
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
this
refers to the "owner" of the function.this
keyword refers to an object
this
refers to the object.this
refers to the global object.this
refers to the global object.this
is undefined.this
refers to the element that received the event.length
property returns the length (size) of an array<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;
document.getElementById("demo").innerHTML = size;
</script>
4
toString()
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
</script>
Banana,Orange,Apple,Mango
join()
join()
method also joins all array elements into a string.toString(),
but in addition you can specify the separator<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
</script>
Banana * Orange * Apple * Mango
pop()
pop()
method removes the last element from an array<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.pop();
document.getElementById("demo2").innerHTML = fruits;
</script>
Banana,Orange,Apple, Mango
Banana,Orange,Apple
push()
push()
method adds a new element to an array (at the end)<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.push("Kiwi");
document.getElementById("demo2").innerHTML = fruits;
</script>
Banana,Orange,Apple,Mango
Banana,Orange,Apple,Mango,Kiwi
shift()
shift()
method removes the first array element and "shifts" all other elements to a lower index.<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.shift();
document.getElementById("demo2").innerHTML = fruits;
</script>
Banana,Orange,Apple,Mango
Orange,Apple,Mango
unshift()
unshift()
method adds a new element to an array (at the beginning), and "unshifts" older elements<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.unshift("Lemon");
document.getElementById("demo2").innerHTML = fruits;
</script>
Banana,Orange,Apple,Mango
Lemon,Banana,Orange,Apple,Mango
delete()
delete()
leaves undefined holes in the array.<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML =
"The first fruit is: " + fruits[0];
delete fruits[0];
document.getElementById("demo2").innerHTML =
"The first fruit is: " + fruits[0];
</script>
The first fruit is: Banana
The first fruit is: undefined
if
Statement:
if
statement to specify a block of JavaScript code to be executed if a condition is true.<p id="demo">Good Evening!</p>
<script>
if (new Date().getHours() < 18) {
document.getElementById("demo").innerHTML = "Good day!";
}
</script>
Good Day!
else
Statement:
else
statement to specify a block of code to be executed if the condition is false.<p id="demo"></p>
<script>
const hour = new Date().getHours();
let greeting;
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
document.getElementById("demo").innerHTML = greeting;
</script>
Good day
else if
Statement:
else if
statement to specify a new condition if the first condition is false.<p id="demo"></p>
<script>
const time = new Date().getHours();
let greeting;
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
document.getElementById("demo").innerHTML = greeting;
</script>
Good day
switch
statement is used to perform different actions based on different conditions.//Syntax
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
<p id="demo"></p>
<script>
let day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is " + day;
</script>
Today is Friday
for
statement creates a loop with 3 optional expressionsExpression 1
is executed (one time) before the execution of the code block.Expression 2
defines the condition for executing the code block.Expression 3
is executed (every time) after the code block has been executed.//Syntax
for (expression 1; expression 2; expression 3){
// code block to be executed
}
<p id="demo"></p>
<script>
let text = "";
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
for in
statement loops through the properties of an Object//Syntax
for (key in object) {
// code block to be executed
}
<p id="demo"></p>
<script>
const person = { fname: "John", lname: "Doe", age: 25 };
let txt = "";
for (let x in person) {
txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
</script>
John Doe 25
for of
statement loops through the values of an iterable object.//Syntax
for (variable of iterable) {
// code block to be executed
}
<p id="demo"></p>
<script>
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
BMW
Volvo
Mini
while
loop loops through a block of code as long as a specified condition is true//Syntax
while (condition) {
// code block to be executed
}
<p id="demo"></p>
<script>
let text = "";
let i = 0;
while (i < 10) {
text += "<br>The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
do while
loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.//Syntax
do {
// code block to be executed
} while (condition);
<p id="demo"></p>
<script>
let text = "";
let i = 0;
do {
text += "<br>The number is " + i;
i++;
} while (i < 10);
document.getElementById("demo").innerHTML = text;
</script>
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
Made By SOU Student for SOU Students