JavaScript is a dynamically typed language. This means that variables can hold values of any type, and the type of a variable can change throughout its lifetime. For example:
let myVariable;
myVariable = 10;
myVariable = "Hello";
In this code, myVariable
first holds a number and then a string. JavaScript uses duck typing, which means that as long as an object has the properties and methods you expect, it can be used in a particular context.
TypeScript adds static typing to JavaScript. Static typing means that you can explicitly define the type of a variable, function parameter, or return value. This helps in catching type - related errors early in the development process. For example:
let myNumber: number = 10;
// The following line would cause a compilation error
// myNumber = "Hello";
Here, the variable myNumber
is explicitly defined as a number
type, and trying to assign a string to it will result in a compilation error.
JavaScript can be used in multiple ways:
<script>
tag.<!DOCTYPE html>
<html>
<body>
<script>
alert('Hello, World!');
</script>
</body>
</html>
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content - Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Since browsers and Node.js do not natively understand TypeScript, it needs to be compiled to JavaScript first.
npm install -g typescript
.ts
file, for example, app.ts
:function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet('John'));
Then compile it using the tsc
command:
tsc app.ts
This will generate a app.js
file that can be run in a browser or with Node.js.
async/await
are used to handle this.function asyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
}
asyncFunction().then((data) => {
console.log(data);
});
interface Person {
name: string;
age: number;
}
function printPerson(person: Person) {
console.log(`${person.name} is ${person.age} years old.`);
}
const person: Person = { name: 'Alice', age: 25 };
printPerson(person);
const myModule = (function() {
let privateVariable = 'Secret';
function privateFunction() {
return privateVariable;
}
return {
publicFunction: function() {
return privateFunction();
}
};
})();
console.log(myModule.publicFunction());
function printValue(value: string | number) {
if (typeof value === 'string') {
console.log(`The string is: ${value}`);
} else {
console.log(`The number is: ${value}`);
}
}
printValue('Hello');
printValue(10);
JavaScript is a powerful and flexible language that has been the backbone of web development for decades. Its dynamic typing allows for quick prototyping and easy - to - write code. However, as projects grow in size and complexity, the lack of static typing can become a liability.
TypeScript, on the other hand, adds static typing to JavaScript, which helps in catching errors early and making the code more maintainable. It also provides features like interfaces and type guards that enhance the development experience.
In general, if you are working on a small - scale project or need to quickly prototype an idea, JavaScript might be the better choice. For large - scale enterprise applications, TypeScript can provide better long - term maintainability and fewer runtime errors.