o7planning

TypeScript typeof operator

  1. typeof operator
  2. instanceof operator

1. typeof operator

The typeof operator returns a string representing the data type of an evaluated value.
Syntax:
let result = typeof aValue;
Example:
typeof_ex1.ts
console.log(typeof 42); // "number"

console.log(typeof 'blubber'); // "string"

console.log(typeof true); // "boolean

let aVariable; // A variable is not initialized.
console.log(typeof aVariable); // "undefined"
Basically, the typeof operator only returns one of the following values:
1
undefined
"undefined"
2
null
"object"
3
Boolean
"boolean"
4
number
"number"
5
BigInt
"bigint"
6
string
"string"
7
Symbol
"symbol"
8
Function object
"function"
9
Any other object
"object"
  • TypeScript BigInt
  • TypeScript Symbol
  • TypeScript Boolean
  • Closures
  • Numbers
  • String
  • Objects
Example:
typeof_ex2.ts
console.log(' --- (1) typeof undefined -- ');
console.log(typeof undefined); //
let aValue; // A variable is not initialized
console.log(typeof aValue);  // "undefined"

console.log(' --- (2) typeof null -- ');
console.log(typeof null); // "object"

console.log(' --- (3) typeof Boolean -- ');
console.log(typeof true); // "boolean"
console.log(typeof (3 > 5)); // "boolean"

console.log(' --- (4) typeof Number -- ');
console.log(typeof 100); // "number"

console.log(' --- (5) typeof bigint -- ');
console.log(' ** See the bigint article for how to use the bigint library.');

console.log(' --- (6) typeof string -- ');
console.log(typeof "Tom"); // "string"

console.log(' --- (7) typeof Symbol -- ');
let aSymbol = Symbol(123);
console.log(typeof aSymbol); // "symbol"

console.log(' --- (8) typeof Function (or Closure) -- ');
let aFunction = function() {
    console.log('Hello');
};
console.log(typeof aFunction); // "function"

console.log(' --- (8) typeof Any-Other-Object -- ');
var anObject1 = {name: 'Tom', salary: 1000};
var anObject2 = new Object();
console.log(typeof anObject1); // "object"
console.log(typeof anObject2); // "object"
Output:
--- (1) typeof undefined --
undefined
undefined
--- (2) typeof null --
object
--- (3) typeof Boolean --
boolean
boolean
--- (4) typeof Number --
number
--- (5) typeof bigint --
** See the bigint article for how to use the bigint library.
--- (6) typeof string --
string
--- (7) typeof Symbol --
symbol
--- (8) typeof Function (or Closure) --
function
--- (8) typeof Any-Other-Object --
object
object
Example: Use the typeof operator to determine the data type passed to a constructor:
typeof_ex3.ts
class Song {
    title: string;
    duration: string; // "minutes:seconds".
     
    constructor(title: string, duration: string | number) {
        this.title = title;
        if(typeof duration === 'string') {
            this.duration = duration;
        } else {
            let seconds = duration % 60;
            let minutes = Math.floor(duration / 60);
            this.duration = minutes + ":" + seconds;
        }
    }
}
let mySong1 = new Song("Hotel California", "6:30");
console.log(`mySong1.duration = ${mySong1.duration}`); // 6:30

let mySong2 = new Song("My Heart Will Go On", 270); // seconds
console.log(`mySong2.duration = ${mySong2.duration}`); // 4:30
Output:
mySong1.duration = 6:30
mySong2.duration = 4:30

2. instanceof operator