ECMAScript is the scripting - language specification developed by Ecma International. JavaScript is the most well - known implementation of the ECMAScript standard. Over the years, different versions of ECMAScript have been released, such as ES5, ES6 (also known as ES2015), ES7, and so on. Each new version brings new features like arrow functions, classes, promises, and async/await.
// ES6 arrow function example
const add = (a, b) => a + b;
console.log(add(2, 3));
TypeScript is a programming language developed and maintained by Microsoft. It is a superset of JavaScript, which means that any valid JavaScript code is also valid TypeScript code. TypeScript adds static typing to JavaScript, allowing developers to define types for variables, functions, and objects. This helps catch errors at compile - time rather than runtime.
// TypeScript function with type annotations
function add(a: number, b: number): number {
return a + b;
}
console.log(add(2, 3));
ECMAScript code can be written directly in HTML files or in separate JavaScript files. To use modern ECMAScript features in the browser, you may need to use a transpiler like Babel to convert the code into a version that older browsers can understand.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
</head>
<body>
<script>
// ECMAScript 6 code
const numbers = [1, 2, 3];
const squared = numbers.map(num => num * num);
console.log(squared);
</script>
</body>
</html>
To write TypeScript code, you first need to install the TypeScript compiler globally using npm:
npm install -g typescript
Then, you can create a .ts
file and write your TypeScript code. After that, you can compile the TypeScript code to JavaScript using the tsc
command.
// example.ts
let message: string = "Hello, TypeScript!";
console.log(message);
To compile the code:
tsc example.ts
This will generate an example.js
file that can be run in a browser or Node.js environment.
// math.js
export function add(a, b) {
return a + b;
}
// main.js
import { add } from './math.js';
console.log(add(2, 3));
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
fetchData();
interface User {
name: string;
age: number;
}
function printUser(user: User) {
console.log(`${user.name} is ${user.age} years old.`);
}
const newUser: User = { name: 'John', age: 30 };
printUser(newUser);
try...catch
blocks when dealing with asynchronous operations to handle potential errors gracefully.interface Person {
address?: {
street?: string;
};
}
const person: Person = {};
const street = person?.address?.street?? 'Unknown';
console.log(street);
any
Type: The any
type defeats the purpose of using TypeScript. Try to define specific types whenever possible.ECMAScript and TypeScript both have their own strengths and use cases. ECMAScript is the foundation of JavaScript and is widely used in web development due to its simplicity and compatibility. It provides a rich set of features for modern web applications. On the other hand, TypeScript adds static typing to JavaScript, which is particularly useful in large - scale projects where code maintainability and error prevention are crucial. By understanding the differences between the two, developers can choose the most appropriate technology for their specific needs.