JavaScript (JS) vs TypeScript (TS) Overview:
2024
- JavaScript: A dynamic, weakly-typed language. Types are determined at runtime, and there’s no built-in support for static type checking.
- TypeScript: A superset of JavaScript that adds static typing (types are enforced at compile time), along with other features like interfaces, classes, and better tooling support. TypeScript compiles down to JavaScript.
Comparison of TypeScript with JavaScript
JavaScript Example:
function add(a,b){
return a+b;
}
console.log(add("hello", "world") ==> helloworld
console.log(add(3, 8) ==> 11
- Problem: The function works, but without type checking, you might unintentionally pass strings instead of numbers, leading to unintended behavior.
TypeScript Example:
function add(a: number , b: number): number{
return (a+b);
}
console.log(add(3, 8) ==> 11
// console.log(add("hello", "world") ==> // TypeScript will throw an error during compilation
Advantage: TypeScript enforces that both a
and b
are numbers, catching type errors at compile time.
Type Annotations in TypeScript vs Python
In TypeScript, types are applied similarly to how Python uses type hints. In TypeScript, you explicitly declare types for variables, function parameters, and return values.
Python Example:
def add(a:int , b: int) -> int :
return (a+b)
Memory updated
JavaScript (JS) vs TypeScript (TS) Overview:
- JavaScript: A dynamic, weakly-typed language. Types are determined at runtime, and there’s no built-in support for static type checking.
- TypeScript: A superset of JavaScript that adds static typing (types are enforced at compile time), along with other features like interfaces, classes, and better tooling support. TypeScript compiles down to JavaScript.
Comparison of TypeScript with JavaScript
JavaScript Example:
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
console.log(add("hello", "world")); // "helloworld"
- Problem: The function works, but without type checking, you might unintentionally pass strings instead of numbers, leading to unintended behavior.
TypeScript Example:
// TypeScript: Adds static typing
function add(a: number, b: number): number {
return a + b;
}
console.log(add(2, 3)); // 5
// console.log(add("hello", "world")); // TypeScript will throw an error during compilation
- Advantage: TypeScript enforces that both
a
andb
are numbers, catching type errors at compile time.
Type Annotations in TypeScript vs Python
In TypeScript, types are applied similarly to how Python uses type hints. In TypeScript, you explicitly declare types for variables, function parameters, and return values.
Python Example:
def add(a: int, b: int) -> int:
return a + b
TypeScript Example:
function add(a: number, b: number): number {
return a + b;
}
Object-Oriented Programming (OOP) in TypeScript vs JS
JS :
class Animal{
constructor(age){
this.age = age
}
speak(){
console.log("Animal"+ this.name + "well")
}
}
class Dog extends Animal{
constructor(name, age){
super(age)
this.name = name
}
sound(){
console.log("Dog"+ this.name + "barking")
}
}
const dg= new Dog("Puppy",23)
dg.speak(); ==> "animal Puppy well"
dg.sound(); ==> "Dog Puppy barking
Typescript:
class Animal{
age: number;
constructor(age: number){
this.age = age;
}
speak: void{
console.log("Dog"+ this.name + "barking")
}
}
class Dog extends Animal{
name: string;
constructor(name: string , age: number){
super(age)
this.name = name;
}
sound() : void{
console.log("Dog"+ this.name + "barking")
}
}
const dg= new Dog("Puppy",23) ;
dg.speak(); ==> "animal Puppy well"
dg.sound(); ==> "Dog Puppy barking