EN
TypeScript - primitive types
3 points
In TypeScript there are available following primitive types.
xxxxxxxxxx
1
let a = 123;
2
let b = 3.14;
3
let c : number = 123;
4
let d : number = 3.14;
5
6
console.log(`a=${a} b=${b} c=${c} d=${d}`);
Output:
xxxxxxxxxx
1
a=123 b=3.14 c=123 d=3.14
xxxxxxxxxx
1
let a = 'Hello world!';
2
let b : string = 'Hello world!';
3
4
console.log(`a='${a}' b='${b}'`);
Output:
xxxxxxxxxx
1
a='Hello world!' b='Hello world!'
xxxxxxxxxx
1
let a = true;
2
let b : boolean = false;
3
4
console.log(`a=${a} b=${b}`);
Output:
xxxxxxxxxx
1
a=true b=false
xxxxxxxxxx
1
function doNothing() : void
2
{
3
// nothing here ...
4
}
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
.
xxxxxxxxxx
1
let a : any = 'text';
2
3
a = 123;
4
a = true;
5
6
console.log(`a=${a}`);
7
8
Output:
xxxxxxxxxx
1
a=true
Note: with this type we can assign anything to variable. It shouldn't be used if it is not necessary.