0% found this document useful (0 votes)
8 views4 pages

Vishal 03 JSV

hii

Uploaded by

mdkhalid8527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Vishal 03 JSV

hii

Uploaded by

mdkhalid8527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name-Vishal Kumar Pandey Roll no.

-11222881 BCSE-703L (Advanced JavaScript Lab)

EXPERIMENT-03
AIM: Write TypeScript code demonstrating static typing and type
inference in JavaScript, including type annotations and interfaces.
Source-Code:

// Simulating static typing with comments


/**
* @type {number}
*/
let age = 20;
/**
* @type {string}
*/
let name = 'vishal';

/**
* @type {boolean}
*/
let isActive = true;

/**
* Function to add two numbers
* @param {number} x
* @param {number} y
* @returns {number}
*/
Name-Vishal Kumar Pandey Roll no.-11222881 BCSE-703L (Advanced JavaScript Lab)

function add(x, y) {
return x + y;
}

// Example usage of the add function


let sum = add(5, 10);
console.log(`Sum: ${sum}`); // Output: Sum: 15

// Simulating an interface-like structure with a function


/**
* @typedef {Object} Person
* @property {string} name
* @property {number} age
* @property {boolean} isActive
*/

/**
* Function to print person details
* @param {Person} person - The person object
*/
function printPerson(person) {
console.log(`Name: ${person.name}, Age: ${person.age}, Active:
${person.isActive}`);
}

// Example usage of the printPerson function


const john = { name: 'John', age: 30, isActive: true };
Name-Vishal Kumar Pandey Roll no.-11222881 BCSE-703L (Advanced JavaScript Lab)

printPerson(john); // Output: Name: John, Age: 30, Active: true

// JavaScript's type inference (loosely simulating TypeScript)


let inferredString = 'Hello';
let inferredNumber = 42;
let inferredBoolean = true;

// Another example of inferred types in an object


const inferredPerson = {
name: 'vishal',
age: 28,
isActive: false
};

printPerson(inferredPerson);
// Using union types in JavaScript (simulated)
/**
* @param {number|string} id - The ID can be either a number or a string
*/
function printId(id) {
if (typeof id === 'string') {
console.log(`ID is a string: ${id.toUpperCase()}`);
} else {
console.log(`ID is a number: ${id}`);
}
}
Name-Vishal Kumar Pandey Roll no.-11222881 BCSE-703L (Advanced JavaScript Lab)

printId(101); // Output: ID is a number: 101


printId("202"); // Output: ID is a string: 202

OUTPUT:

You might also like