Languages
[Edit]
EN

TypeScript - primitive types

3 points
Created by:
AnnLen
1771

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 only null or undefined.

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.

 

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.