Home - CodeWithHarry
JavaScript Basics
Set of JavaScript basic syntax to add, execute and write basic programming paradigms in
Javascript
On Page Script
Adding internal JavaScript to HTML
<script type="text/javascript"> //JS code goes here </script>
External JS File
Adding external JavaScript to HTML
<script src="filename.js"></script>
Functions
JavaScript Function syntax
function nameOfFunction () {
// function body
}
DOM Element
Changing content of a DOM Element
document.getElementById("elementID").innerHTML = "Hello World!";
Output
This will print the value of a in JavaScript console
console.log(a);
Conditional Statements
1/15
, Home - CodeWithHarry
Conditional statements are used to perform operations based on some conditions.
If Statement
The block of code to be executed, when the condition specified is true.
if (condition) {
// block of code to be executed if the condition is true
}
If-else Statement
If the condition for the if block is false, then the else block will be executed.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Else-if Statement
A basic if-else ladder
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
} else {
// block of code to be executed if the condition1 is false and condition2 is
}
Switch Statement
Switch case statement in JavaScript
switch(expression) {
case x:
// code block
break;
case y:
2/15
JavaScript Basics
Set of JavaScript basic syntax to add, execute and write basic programming paradigms in
Javascript
On Page Script
Adding internal JavaScript to HTML
<script type="text/javascript"> //JS code goes here </script>
External JS File
Adding external JavaScript to HTML
<script src="filename.js"></script>
Functions
JavaScript Function syntax
function nameOfFunction () {
// function body
}
DOM Element
Changing content of a DOM Element
document.getElementById("elementID").innerHTML = "Hello World!";
Output
This will print the value of a in JavaScript console
console.log(a);
Conditional Statements
1/15
, Home - CodeWithHarry
Conditional statements are used to perform operations based on some conditions.
If Statement
The block of code to be executed, when the condition specified is true.
if (condition) {
// block of code to be executed if the condition is true
}
If-else Statement
If the condition for the if block is false, then the else block will be executed.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Else-if Statement
A basic if-else ladder
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
} else {
// block of code to be executed if the condition1 is false and condition2 is
}
Switch Statement
Switch case statement in JavaScript
switch(expression) {
case x:
// code block
break;
case y:
2/15