Annotation in Javascript and Typescript

Aartiyadav

Neues Mitglied
Hello All, I want to know who needs annotation between javascript and typescript. I am working on Random Quote Generator Javascript Project and Some parts require typescript functionality. According to this reference, JavaScript does not require annotation but not an idea about typescript. Can anyone know about them? Or give me any example of annotation in typescript.
 
In JavaScript and TypeScript, annotations are typically used to provide type information to variables, parameters, and functions. Type annotations allow developers to specify the expected type of a value or object, which can help catch errors early and improve code readability.

In JavaScript, type annotations can be added using JSDoc comments, which are specially formatted comments that begin with the /** syntax. For example, the following code uses a JSDoc comment to annotate the add function with the expected types of its parameters and return value:

In JavaScript:
Javascript:
/**
 * Adds two numbers and returns the result.
 * @param {number} a - The first number to add.
 * @param {number} b - The second number to add.
 * @returns {number} The sum of a and b.
 */
function add(a, b) {
  return a + b;
}

TypeScript annotations are built into the language and specified using a colon 🙂) followed by the type name. For example, the following TypeScript code annotates the add function with the expected types of its parameters and return value:

function add(a: number, b: number): number {
return a + b;
}

TypeScript also supports more advanced annotations, such as interface and class declarations, that allow developers to define custom types and structures. Overall, annotations are essential for ensuring code correctness and improving the maintainability of JavaScript and TypeScript applications. Found this article on the internet about Typescript and JavaScript; you can take reference from here, and I hope this will help you to understand.
 

Neue Themen


Zurück
Oben