EN
TypeScript - primitive types
3
points
In TypeScript there are available following primitive types.
1. number
type example
let a = 123;
let b = 3.14;
let c : number = 123;
let d : number = 3.14;
console.log(`a=${a} b=${b} c=${c} d=${d}`);
Output:
a=123 b=3.14 c=123 d=3.14
2. string
type example
let a = 'Hello world!';
let b : string = 'Hello world!';
console.log(`a='${a}' b='${b}'`);
Output:
a='Hello world!' b='Hello world!'
3. boolean
type example
let a = true;
let b : boolean = false;
console.log(`a=${a} b=${b}`);
Output:
a=true b=false
4. void
type example
function doNothing() : void
{
// nothing here ...
}
Note: this type is useful with functions. It is possible to create variable with
void
type (let value : void = null
) but it allows later to assign onlynull
orundefined
.
5. any
type example
let a : any = 'text';
a = 123;
a = true;
console.log(`a=${a}`);
Output:
a=true
Note: with this type we can assign anything to variable. It shouldn't be used if it is not necessary.