Senior
What is the difference between TypeScript and JavaScript?
sobes.tech AI
Answer from AI
TypeScript is a superset of JavaScript that adds static typing and additional features for developing large applications. Main differences:
- Static typing: TypeScript allows explicitly specifying types for variables, functions, and objects, which helps catch errors at compile time.
- Compilation: TypeScript code is compiled into plain JavaScript, which can be understood by any browser or runtime environment.
- Support for modern features: TypeScript supports new ECMAScript standards and additional syntactic constructs, such as interfaces, enums, and generics.
Example:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("World"));
In JavaScript, the equivalent would look like this, but without type checking:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
Thus, TypeScript helps write more reliable and maintainable code, especially in large projects.