Number vs number in TypeScript
Created by Canva

Number vs number in TypeScript

In TypeScript, number refers to the primitive type that represents a numeric value, while Number refers to the global object that can be used to perform mathematical operations and manipulate numeric values.

The number type is used to define variables or function parameters that can only accept numeric values, such as:

let age: number = 30;
function add(a: number, b: number): number {
 return a + b; 
}         

On the other hand, Number is a wrapper object that can be used to manipulate numeric values, such as:

const x: Number = new Number(10);
const y: Number = new Number(20);
const z: number = x.valueOf() + y.valueOf();c
onsole.log(z); // 30         

However, it is generally recommended to use the number type instead of the Number object in TypeScript, as it is a more lightweight and efficient way of working with numeric values.

要查看或添加评论,请登录

Tamjid Ahmed的更多文章

社区洞察

其他会员也浏览了