We must enclose the JavaScript code within the <script> tag in order to include it on an HTML page just as the example shown below
<script type = "text/javascript">
//JavaScript coding can be done inside this tag
<script>
An external JavaScript file can also be written separately and included within our HTML file. That way, different types of code can be kept isolated from one another, resulting in better-organised files. For instance, if our JavaScript code is written in the file script.js, we can include it in our HTML file in the following way:
<script src="script.js"><script>
Variables in JavaScript are simply names of storage locations. In other words, they can be considered as stand-in values that we can use to perform various operations in our JavaScript codes.
var x = 140;
The most commonly used variable in JavaScript is var. It can be redeclared and its value can be reassigned, but only inside the context of a function. When the JavaScript code is run, variables defined using var are moved to the top.
const x = 5;
const: const variables in JavaScript cannot be used before they appear in the code. They can neither be reassigned values, that is, their value remains fixed throughout the execution of the code, nor can they be redeclared.
let x = 202;
let: The let variable, like const, cannot be redeclared. But they can be reassigned a value.
var id = 100
Numbers: These are just numerical values. They can be real numbers or integers.
var y
Variables
var demoString = "Hello World"
Text(Strings)
sum = 20 + 30 + 29
Operations
booleanValue = true
Boolean Values
const g = 9.8
Constant Numbers
var name = {name:"Jon Snow", id:"AS123"}
Objects
Fundamental Operators
let x = 5; let y = 2; let z = x + y;
Addition Operator
let x = 5; let y = 2; let z = x - y;
Substraction Operator
let x = 5; let y = 2; let z = x * y;
Multiplication Operator
let x = 6; let y = 2; let z = x / y;
Division Operator
let x = 5; let y = 2; let z = x % y;
Modulas Operator
let x =2; x++;
Increment Operator
let x =2; x++;
Decrement Operator
let x = 5; let y = 2; let z = x ** y;
Exponential Operator
Bitwise Operators
let x = 5 & 1;
bitwise AND
let x = 5 | 1;
bitwise OR
let x = 5 ^ 1;
Bitwise XOR
let x = ~5;
Bitwise NOT
let x = 5 << 1;
Bitwise Left Shift
let x = -5 >> 1;
Bitwise Right Shift
Comparision Operators:Used to test true or false.
x == 8
equal to
x === 8
equal value and equal type
x != 8
not equal
x !== 8
not equal value or not equal type
8 > 6
greater than
5 < 8
less than
8 >= 8
greater than or equal to
8 <= 9
less than or equal to
Logical Operator
5 < 10 && 2 > 1
Logical AND
x == 5 || y == 5
Logical OR
!(9 == 8)
Logical NOT
JavaScript If-Else Statements
if (check condition) { // block of code to be executed if the given condition is satisfied } else { // block of code to be executed if the given condition is not satisfied }
for (initialization of the loop variable; condition checking for the loop; updation after the loop) { // code to be executed in loop }
for loop
// Initialization of the loop variable is done before the while loop begins while(condition checking for the loop){ // 1. code to be executed in loop // 2. updation of the loop variable }
while loop
// Initialization of the loop variable is done before the do-while loop begins do{ // 1. code to be executed in loop // 2. updation of the loop variable }while(condition checking for the loop);
do-while loop
Arrays
var cars = ["Mercedes", "Tesla","Volvo"];
Functions
function nameOfTheFunction(parameterOne, parameterTwo, parameterThree, parameterFour,....,parameterN) {
// Job or Task of the function
}
var hello = 'Hello!'; function sayHello() { console.log(hello); } // 'Hello!' gets logged sayHello();
Global Scope
function sayHello() { var hello = 'Hello!'; console.log(hello); } // 'Hello!' gets logged sayHello();
Local or Function Scope
/{ let hello = 'Hello!'; var language = 'Hindi'; console.log(hello); // 'Hello!' gets logged } console.log(language); // 'Hindi!' gets logged console.log(hello); // Uncaught ReferenceError: hello is not defined
Block Scope
let a = 'a';
function foo() {
let b = 'b';
console.log(b); // 'b' gets logged
console.log(a); // 'a' gets logged
randomNumber = 33;
console.log(randomNumber); // 33 gets logged
}
foo();
display("Lion"); function display(inputString) { console.log(inputString); // 'Lion' gets logged }
Function Hoisting
console.log(x) // 'undefined' is logged from hoisted var declaration (instead of 7) var x // Declaration of variable x x = 7; // Initialization of variable x to a value 7 console.log(x); // 7 is logged post the line with initialization's execution.
Variable Hoisting
We must enclose the JavaScript code within the <script> tag in order to include it on an HTML page just as the example shown below
function subtractor(subtractingInteger) {
return function(a) {
return a - subtractingInteger;
};
}
var subtract2 = subtractor(2);
var subtract5 = subtractor(5);
console.log(subtract2(5)); // 3 is logged
console.log(subtract5(5)); // 0 is logged