UNIT 3 &4 NOTES
UNIT 3 &4 NOTES
o While const ensures the variable reference cannot change, it does not make
objects or arrays immutable. It only prevents reassigning the variable itself to a
new value, but the contents of an object or array declared with const can still be
modified
3 Write the program for how to let function return a value
function add(a, b) {
return a + b; // The function returns the sum of a and b
}
let result = add(3, 4); // Calling the function with 3 and 4 as arguments
console.log(result); // Output: 7
4 Define javascript Array object.
In JavaScript, an Array is a built-in object that allows you to store multiple values in a single
variable. It is a collection of ordered elements, which can be of any type (numbers, strings,
objects, etc.). Arrays are zero-indexed, meaning the first element is at index 0.
5 What is Event Bubbling?
Event Bubbling is a concept in JavaScript where an event triggered on an element (e.g., a button
or a div) is first handled by that element and then propagates (or "bubbles up") through its
ancestor elements in the DOM hierarchy, all the way up to the root (usually the document or
window object).In simpler terms, when an event is triggered on an element, it doesn't just stay
with that element. The event "bubbles" up to its parent, then to the parent's parent, and so on.
This allows for event delegation, where you can set up event listeners on parent elements and
handle events for multiple child elements.
6 What are the features of javascript?
JavaScript is a powerful, flexible, and widely-used programming language primarily for web
development. It provides a range of features that make it highly versatile. Below are some key
features:
1. Interpreted Language:
o JavaScript is an interpreted language, meaning it doesn't need to be compiled
before execution. The browser or JavaScript engine directly interprets and runs
the code line by line.
2. Dynamic Typing:
o JavaScript is dynamically typed, meaning you don't need to declare the type of a
variable explicitly. The type of a variable is determined at runtime.
7 Differentiate javascript and typescript.
10 Define Namespace .
A Namespace in TypeScript is a way to group related functionality, such as variables, functions, classes,
and interfaces, into a single logical unit. It helps to organize code and avoid naming conflicts by
encapsulating the code within a specific scope.
Math.PI → 3.141592653589793
Math Methods
Math.ceil(x) → Rounds up
console.log(Math.ceil(4.1)); // 5
String Format
let dateStr = new Date("2024-04-02T10:00:00");
console.log(dateStr);
Date Methods
Date Formatting
console.log(now.toDateString()); // "Tue Apr 02 2024"
console.log(now.toISOString()); // "2024-04-02T10:00:00.000Z"
Date Calculations
let future = new Date();
future.setDate(future.getDate() + 7); // Adds 7 days
console.log(future);
12 a Explain non primitive data types of Typescript
In TypeScript, non-primitive data types are more complex than primitive types. They
include objects, arrays, functions, tuples, and classes.
1. Object (3 Marks)
An object is a collection of key-value pairs. It can store multiple values of different types.
Example:
let person: { name: string; age: number; isStudent: boolean } = {
name: "John",
age: 25,
isStudent: true
};
console.log(person.name); // "John"
Objects can also have optional properties using ?:
let car: { brand: string; model?: string } = { brand: "Toyota" };
2. Array (3 Marks)
An array is a collection of elements of the same type. TypeScript provides strong typing
for arrays.
Example:
let numbers: number[] = [10, 20, 30];
let names: Array<string> = ["Alice", "Bob"];
Operations on Arrays:
numbers.push(40); // Adds an element
console.log(numbers.length); // 4
3. Tuple (2 Marks)
A tuple is a fixed-length array where each element has a specific type.
Example:
let employee: [string, number] = ["John", 25];
console.log(employee[0]); // "John"
console.log(employee[1]); // 25
Tuples allow optional elements:
let data: [string, number?] = ["Alice"];
4. Function (2 Marks)
Functions in TypeScript have defined parameter and return types.
Example:
function add(a: number, b: number): number {
return a + b;
}
console.log(add(5, 10)); // 15
Functions can also have optional parameters:
function greet(name: string, age?: number): string {
return age ? `Hello, ${name}, you are ${age} years old.` : `Hello, $
{name}`;
}
5. Class (3 Marks)
A class is a blueprint for creating objects. It supports encapsulation, inheritance, and
polymorphism.
Example:
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
makeSound(): void {
console.log(`${this.name} makes a sound`);
}
}
// Using Namespace
console.log(MathOperations.add(10, 5)); // 15
console.log(MathOperations.subtract(10, 5)); // 5
Nested Namespaces:
namespace Company {
export namespace HR {
export function getEmployeeCount(): number {
return 50;
}
}
}
console.log(Company.HR.getEmployeeCount()); // 50
Differences Between Modules and Namespaces
Feature Modules Namespaces
Usage Preferred in modern TypeScript Used in legacy TypeScript
Scope Works at file/module level Works at global level
Import Required? Yes (import/export) No (Global access)
Compatibility Works with JavaScript TypeScript-specific
13 a (i) How to pass variables to a function and use these variables values in the function
In TypeScript (or JavaScript), we can pass variables to a function as parameters. These
variables can then be used inside the function for processing.
1. Function with Parameters (3 Marks)
We define function parameters and pass values when calling the function.
Example:
function greet(name: string): void {
console.log("Hello, " + name);
}
constructor(item: T) {
this.item = item;
}
getItem(): T {
return this.item;
}
}
uniqueNumbers.add(5);
console.log(uniqueNumbers); // Output: {1, 2, 3, 4, 5}
Prevents duplicates.
Supports methods like add(), delete(), and has().
4. Maps (2 Marks)
A Map stores key-value pairs, where keys can be any type (unlike objects, where keys
must be strings).
Example:
let studentScores: Map<string, number> = new Map();
studentScores.set("Alice", 90);
studentScores.set("Bob", 85);
console.log(studentScores.get("Alice")); // Output: 90
Keys can be strings, numbers, or objects.
Supports methods like set(), get(), and has()