Web Scripting Updated
Web Scripting Updated
- RONAK BHALALA
History and Evolution of javascript
History and Evolution of JavaScript
● Download Visual Studio Code. even you can use normal notepad also.
2. In the VS Code editor, go to File > Open Folder and select the folder you just created (MyJavaScriptProject).
3. Inside this folder, create a new file and name it index.html.
1. Open any webpage in your browser (you can use a blank page).
2. Open the Developer Tools (press F12 or right-click and select Inspect).
3. Go to the Console tab.
4. Type console.log("Hello, World!"); and press Enter. You will see Hello, World! printed in the console.
Conclusion
You can now run JavaScript in a browser using the <script> tag in an HTML file. No need for frameworks—just pure
JavaScript!
Variables in JavaScript
In JavaScript, variables are used to store and manipulate data. You can create a variable using one of three keywords: var,
let, or const. Each has different scoping rules and behaviors.
● Scope: var has function scope (if declared inside a function) or global scope (if declared outside any function).
● Hoisting: Variables declared with var are hoisted to the top of their scope, meaning they are accessible before they
are declared, but their value will be undefined until the line of declaration is executed.
● Scope: let has block scope. This means it is limited to the block in which it is defined (e.g., inside loops or
conditionals).
● Hoisting: let is also hoisted but remains in a temporal dead zone until it is initialized. It cannot be accessed before
its declaration.
Null & Undefined: Special types to represent "no value" or "missing value".
let emptyValue = null;
let uninitializedValue;
Setting up Development environment
// String: Text data enclosed in quotes.
let name = "John";
console.log(name); // Output: John
// Null & Undefined: Special types to represent "no value" or "missing value".
let emptyValue = null;
console.log(emptyValue); // Output: null
let uninitializedValue;
console.log(uninitializedValue); // Output: undefined
Setting up Development environment
Use let for variables that can change.
Use const for variables that should remain constant.
Avoid using var unless necessary (older codebases or specific scenarios).
var: Can be used anywhere in the function or globally. You can reassign its value. It gets moved to the top of the code but will be
undefined until it’s actually assigned.
let: Can be used only within the block (like inside loops or conditions). You can reassign its value. It gets moved to the top but can’t be
used before the declaration.
const: Can be used only within the block. You can’t reassign its value once it's set. It must be assigned a value when declared.
Extra
For writing into document:
<script> document.write("Hello World"); </script> /
These are simple data types that are immutable and hold a single value. They are directly assigned to variables.
a) Number
Operations:
● Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%), Exponentiation (**)
● let sum = 10 + 5; // 15
● let product = 10 * 5; // 50
● let remainder = 10 % 3; // 1
Data Types and operators
● b) String
● Represents a sequence of characters enclosed in single, double, or backtick quotes.
Operations:
● Concatenation (+)
● String length (.length)
● String methods (e.g., .toUpperCase(), .toLowerCase())
d) Undefined
Indicates that a variable has been declared but not assigned a value.
let x;
console.log(x); // undefined
Operations:
● Undefined can be assigned to variables, but cannot be operated on like a number or string.
Data Types and operators
f) Symbol (ES6)
● A unique and immutable primitive value used as the key for object properties.
Operations:
● Cannot be used with arithmetic operations or string concatenation. Primarily used for creating unique keys in objects.
g) BigInt (ES11)
Used to represent integers that are too large for the Number type
Operations:
● Supports arithmetic operations but cannot be mixed with regular Number type.
Data Types and operators
e) Null
let person = {
name: "John",
age: 30
};
function greet() {
console.log("Hello!");
}
Data Types and operators
Operations on Data Types in JavaScript:
let sum = 10 + 5; // 15
let diff = 10 - 5; // 5
let product = 10 * 5; // 50
let quotient = 10 / 5; // 2
let remainder = 10 % 3; // 1
let power = 2 ** 3; // 8
Data Types and operators
String Operations:
let a = true;
let b = false;
let andResult = a && b; // false
let orResult = a || b; // true
let notResult = !a; // false
Type Conversion:
arr.push(4); // [1, 2, 3, 4]
arr.pop(); // [1, 2, 3]
arr.shift(); // [2, 3]
arr.unshift(0); // [0, 2, 3]
Object Operations:
console.log(obj.name); // "Alice"
1. Conditional Statements
These are used to perform different actions based on different conditions.
if (condition) {
● else Statement: Executes a block of code if the condition in the if statement is false.
if (condition) {
} else {
}
Control structure
● else if Statement: Specifies a new condition if the original if condition is false.
if (condition1) {
} else if (condition2) {
} else {
}
Control structure
Example:
User define example
let number = -5; // Prompt the user to enter a number
let number = parseInt(prompt("Enter a number:"));
// Check if the number is positive, negative, or zero
if (number > 0) {
if (number > 0) {
console.log("You entered the number: " + number);
console.log("The number is positive.");
console.log("The number is positive.");
alert("You entered the number: " + number + "\nThe
} else { number is positive.");
} else if (number < 0) {
console.log("The number is negative."); console.log("You entered the number: " + number);
console.log("The number is negative.");
} alert("You entered the number: " + number + "\nThe
number is negative.");
} else {
console.log("You entered the number: " + number);
console.log("The number is zero.");
alert("You entered the number: " + number + "\nThe
number is zero.");
}
Control structure
● switch Statement: Evaluates an expression and matches the result to one of several case blocks.
switch (expression) {
case value1:
break;
case value2:
break;
default:
}
Control structure
Example:
User define example
let number = 4; // Prompt the user to enter a number
let number = parseInt(prompt("Enter a number:"));
switch (number % 2) {
// Display the entered number
case 0: console.log("Even"); break;
console.log("You entered the number: " + number);
case 1: console.log("Odd"); break;
// Check if the number is odd or even
default: console.log("Invalid"); switch (number % 2) {
case 0:
} alert("The number " + number + " is Even.");
break;
case 1:
alert("The number " + number + " is Odd.");
break;
default:
alert("Invalid input.");
}
Control structure
2. Looping Statements
}
Control structure
User define example
Example: // Get the height from the user
let height = parseInt(prompt("Enter the height of the
pyramid:"));
let height = 5; // You can change this value to
increase or decrease the size of the pyramid // Check if the height is a valid number
if (isNaN(height) || height <= 0) {
console.log("Please enter a valid positive number.");
for (let i = 1; i <= height; i++) { } else {
let row = ''; // Generate the pyramid
for (let i = 1; i <= height; i++) {
let row = '';
// Add leading spaces
for (let j = 1; j <= height - i; j++) { // Add leading spaces
row += ' '; for (let j = 1; j <= height - i; j++) {
row += ' ';
}
}
while (condition) {
Ex: let i = 0;
i++;
}
Control structure
User define example
Example: let number = parseInt(prompt("Enter a number to
let number = 5; // Static number for which factorial is calculate its factorial:"));
calculated let factorial = 1;
let factorial = 1;
if (number < 0) {
if (number < 0) { console.log("Factorial is not defined for negative
console.log("Factorial is not defined for negative numbers.");
numbers."); } else {
} else { let i = 1;
let i = 1; while (i <= number) {
while (i <= number) { factorial *= i;
factorial *= i; i++;
i++; }
} console.log(`The factorial of ${number} is
console.log(`The factorial of ${number} is ${factorial}`);
${factorial}`); }
}
Control structure
● do...while Loop: Executes a block of code once, and then repeats the execution as long as the specified condition is true.
while (condition) {
Ex: let i = 0;
do {
i++;
if (i === 5) {
console.log(i);
}
Control structure
● continue Statement: Skips the current iteration of a loop and continues with the next iteration.
if (i === 5) {
console.log(i);
}
Control structure
● return Statement: Exits from the current function and optionally returns a value.
function add(a, b) {
}
Control structure
In JavaScript, there is no typeof operator for directly
checking the type of a variable. However, JavaScript let isTrue = true;
provides the typeof operator, which can be used to let obj = { name: "John", age: 30 };
check the type of a variable at runtime.
let arr = [1, 2, 3];
1. typeof Operator:
let func = function() {};
The typeof operator in JavaScript is used to check
console.log(typeof num); // "number"
the data type of a variable or an expression.
console.log(typeof str); // "string"
Syntax:
console.log(typeof isTrue); // "boolean"
typeof variable
console.log(typeof obj); // "object"
let num = 10;
console.log(typeof arr); // "object"
let str = "Hello, World!"; (arrays are also objects in JavaScript)
console.log(typeof undefinedVar); //
"undefined"