JavaScript is a scripting language that is primarily used for creating dynamic web pages. It is an untyped language, which means you don’t need to specify the data type of a variable when you declare it. For example:
let message = "Hello, World!";
let num = 42;
Here, the variable message
holds a string, and num
holds a number, but we didn’t explicitly define their types. JavaScript uses dynamic typing, which allows variables to change their type during runtime.
TypeScript, being a superset of JavaScript, includes all the features of JavaScript and adds static typing. Static typing means that you can specify the data type of a variable when you declare it. For example:
let message: string = "Hello, World!";
let num: number = 42;
The : string
and : number
notations are type annotations. TypeScript uses a compiler to check the types at compile - time, which helps in catching type - related errors early in the development process.
JavaScript can be used in multiple ways:
<script>
tag.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
</head>
<body>
<button id="myButton">Click me</button>
<script>
const button = document.getElementById('myButton');
button.addEventListener('click', function() {
alert('Button clicked!');
});
</script>
</body>
</html>
.js
files and link them to your HTML files.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
</head>
<body>
<button id="myButton">Click me</button>
<script src="script.js"></script>
</body>
</html>
In the script.js
file:
const button = document.getElementById('myButton');
button.addEventListener('click', function() {
alert('Button clicked!');
});
Since browsers cannot directly execute TypeScript code, you need to compile it to JavaScript first.
npm install -g typescript
.ts
file, for example, app.ts
.function greet(name: string) {
return `Hello, ${name}!`;
}
const message = greet('John');
console.log(message);
tsc
command.tsc app.ts
This will generate an app.js
file that can be used in a web page or a Node.js application.
const
and let
: Instead of using var
, use const
for variables that won’t change and let
for variables that will change. This helps in avoiding issues related to variable hoisting.const PI = 3.14;
let counter = 0;
const numbers = [1, 2, 3];
const squared = numbers.map(num => num * num);
function add(a: number, b: number): number {
return a + b;
}
interface Person {
name: string;
age: number;
}
function describePerson(person: Person) {
return `${person.name} is ${person.age} years old.`;
}
// utils.js
function sum(a, b) {
return a + b;
}
// main.js
import { sum } from './utils.js';
const result = sum(3, 5);
try...catch
blocks to handle errors gracefully.try {
const data = JSON.parse('invalid json');
} catch (error) {
console.error('Error parsing JSON:', error);
}
const num = 10; // TypeScript infers the type as number
enum Color {
Red,
Green,
Blue
}
const myColor: Color = Color.Green;
JavaScript and TypeScript both have their own strengths. JavaScript is simple, flexible, and widely supported, making it a great choice for quick prototyping and small - scale projects. TypeScript, on the other hand, adds static typing, which helps in building large - scale, maintainable, and error - free applications. By understanding the differences, usage methods, common practices, and best practices of both languages, developers can choose the right tool for the job and write high - quality code.