Java Script End Sem 2nd Part
Java Script End Sem 2nd Part
interactivity to web pages. It is an essential part of web development and is widely used alongside
HTML and CSS to create dynamic content on the client side (browser) and for server-side
development with environments like Node.js.
Advantages of JavaScript
• Versatile and Cross-Platform: JavaScript can run on various platforms and devices, including
desktops, servers, and mobile devices, making it ideal for developing cross-platform
applications.
• Client-Side Execution: JavaScript is executed in the browser, reducing server load and
improving user experience by allowing immediate feedback.
• Rich User Interfaces: JavaScript enables developers to create interactive elements like
sliders, pop-ups, and dynamic forms that enhance the user experience.
• Large Ecosystem: It boasts a vast library and framework ecosystem (like React, Angular,
Vue.js) that speeds up development.
Definition Code that executes in the user's web browser Code that runs on the web server
Language
JavaScript PHP, Python, Ruby, Node.js, ASP.NET
Examples
Execution Control The user’s browser controls execution The web server controls execution
User Interaction Provides immediate feedback (e.g., form validation) Fetches data, validates, and processes requests
1. Arithmetic Operators
Operator Description Example
+ Addition 5 + 3 results in 8
- Subtraction 5 - 3 results in 2
* Multiplication 5 * 3 results in 15
% Modulus 5 % 2 results in 1
** Exponentiation 2 ** 3 results in 8
2. Assignment Operators
3. Comparison Operators
` `
5. Bitwise Operators
Operator Description Example
` ` Bitwise OR
BigInt Represents whole numbers larger than Number.MAX_SAFE_INTEGER. let bigInt = 9007199254740991n;
Object A collection of properties, which can include primitives and other objects. let obj = { key: "value" };
Array A special type of object used to store multiple values in a single variable. let arr = [1, 2, 3];
An event in JavaScript is an action or occurrence that happens in the browser which the code can
respond to. Events are triggered by user interactions (like mouse clicks, keyboard input), browser
actions (page load), or JavaScript code itself.
Events enable interactive web pages by allowing scripts to execute code when these interactions or
actions occur.
Event
Event Name(s) Description
Category
A recursive function is a function that calls itself in order to solve a problem. This approach is
commonly used to break down complex problems into simpler sub-problems. Recursive functions
have two main components:
1. Base Case: A condition that allows the function to stop calling itself, preventing infinite
recursion.
2. Recursive Case: The part of the function where it calls itself with modified arguments,
bringing it closer to the base case.
The Fibonacci series is a sequence where each number is the sum of the two preceding ones, usually
started with 0 and 1. The sequence looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Here is a JavaScript implementation to generate the Fibonacci series using a recursive function:
EXAMPLE:
function fibonacci(n) {
if (n === 0) {
return 0; }
if (n === 1) {
return 1; }
// Recursive case: Return the sum of the two preceding Fibonacci numbers
function printFibonacciSeries(terms) {
console.log(fibonacci(i)); } }
Definition A variable declared inside a function or block. A variable declared outside any function or block.
Declaration Typically declared with var, let, or const inside Declared with var, let, or const outside any function or
Keywords functions/blocks. block (or implicitly by not declaring in strict mode).
Memory allocated when function is called and Memory allocated when script loads and persists until the
Memory Usage
released after function execution completes. script ends.
Variable Local variables can shadow (override) global Global variables accessible unless shadowed by local
Shadowing variables within their scope. variables.
javascript function foo() { let x = 10; console.log(x); } javascript let y = 20; function bar() { console.log(y); } // y is
Example
// x is local to foo global
A User-Defined Function (UDF) in JavaScript is a function that you create to perform specific tasks.
Unlike built-in functions (like Math.random() or alert()), UDFs allow you to encapsulate reusable
code, making your programs modular and easier to manage.
Components of a UDF:
1. Function Declaration: This is where you define the function using the function keyword.
return area;
let length = 5;
let width = 4;
console.log(`The area of the rectangle is: ${area}`); // Output: The area of the rectangle is: 20
1. Odd or Even:
function isOddEven(num) {
if (num % 2 === 0) {
return "Even";
} else {
return "Odd";
console.log(isOddEven(7)); // Odd
console.log(isOddEven(10)); // Even
2. Multiplication Table
function multiplicationTable(num) {
multiplicationTable(5);
3. Factorial:
function factorial(n) {
console.log(factorial(5)); // 120
function isPrime(num) {
return true;
console.log(isPrime(7)); // true
console.log(isPrime(10)); // false
5. Series of Prime Numbers (up to n)
function primeSeries(n) {
if (isPrime(i)) {
console.log(i);
primeSeries(20);
6. Reverse a Number
function reverseNumber(num) {
let reversed = 0;
return reversed;
console.log(reverseNumber(12345)); // 54321
function isPalindrome(input) {
console.log(isPalindrome("madam")); // true
console.log(isPalindrome(12321)); // true
console.log(isPalindrome("hello")); // false