ES6, also known as ECMAScript 2015, is a major update to the JavaScript language specification. It introduced a plethora of new features such as arrow functions, template literals, destructuring assignment, classes, and the Promise
object. These features aimed to make JavaScript more expressive, easier to read, and less error - prone. ES6 code is natively supported in modern browsers and Node.js environments without any additional compilation steps in most cases.
Here is an example of an ES6 arrow function:
// ES6 arrow function
const add = (a, b) => a + b;
console.log(add(3, 5));
TypeScript is a statically typed superset of JavaScript. It means that all valid JavaScript code is also valid TypeScript code. TypeScript adds static typing to JavaScript, which allows developers to catch type - related errors at compile - time rather than at runtime. This helps in building more robust and scalable applications, especially in large - scale projects. TypeScript code needs to be transpiled into plain JavaScript code before it can be run in a browser or Node.js environment.
Here is an example of a TypeScript function with type annotations:
// TypeScript function with type annotations
function add(a: number, b: number): number {
return a + b;
}
console.log(add(3, 5));
<script>
tag. Most modern browsers support ES6 features natively.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
</head>
<body>
<script>
const message = 'Hello, ES6!';
console.log(message);
</script>
</body>
</html>
node
command.// example.js
const greet = (name) => `Hello, ${name}!`;
console.log(greet('John'));
Run it with:
node example.js
npm install -g typescript
tsconfig.json
file in your project root to configure the TypeScript compiler options.{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist"
}
}
.ts
file and compile it using the tsc
command.// example.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet('John'));
Compile it with:
tsc example.ts
This will generate a JavaScript file in the dist
directory (as specified in tsconfig.json
). You can then run the JavaScript file using Node.js.
import
and export
keywords to manage code modularity.// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// main.js
import { add, subtract } from './math.js';
console.log(add(5, 3));
console.log(subtract(5, 3));
async/await
for handling asynchronous operations in a more synchronous - looking way.function fetchData() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
}
async function main() {
const data = await fetchData();
console.log(data);
}
main();
interface Person {
name: string;
age: number;
}
function greetPerson(person: Person): string {
return `Hello, ${person.name}! You are ${person.age} years old.`;
}
const john: Person = { name: 'John', age: 30 };
console.log(greetPerson(john));
function printValue(value: string | number) {
if (typeof value === 'string') {
console.log(value.toUpperCase());
} else {
console.log(value.toFixed(2));
}
}
printValue('hello');
printValue(3.14159);
const
and let
: Prefer const
for variables that won’t change and let
for variables that need to be reassigned instead of using var
.const PI = 3.14;
let counter = 0;
tsconfig.json
to enforce strict type checking and catch more errors early.{
"compilerOptions": {
"strict": true
}
}
ES6 and TypeScript serve different purposes in the JavaScript ecosystem. ES6 provides modern features to make JavaScript code more concise and maintainable, and it can be used directly in browsers and Node.js without compilation in most cases. TypeScript, on the other hand, adds static typing to JavaScript, which helps in catching type - related errors at compile - time and building more robust applications, especially for large - scale projects. Understanding the differences between them and their respective best practices will allow you to choose the right tool for your specific development needs.