Typescript typeof, instanceOf operator examples


typeof operator introduction:

typeof and  instanceof tutorials

This operator used to get a string specifying the type of variable.  This will very helpful for developers to know the type of the object at runtime.

typeof operator syntax

typeof {operand}

parameter operand is supplied either as the primitive data type of custom object. possible operand values are variables, object variables, and strings.

var msg = “Cloudhadoop”;

console.log(typeof msg); // returns string as string format

var variableNumber =16;

console.log(typeof variableNumber); // returns number as string format

var variableBoolean =true;

console.log(typeof variableBoolean); // returns boolean as string format

var variableNull =null;

console.log(typeof variableNull); // returns object as string format

var variableArray:string[]= [“1”,“2”,“3”,“4”]

console.log(typeof variableArray); // returns object as string format

let variableTyple: [string, number]= [“type operator”, 20];

console.log(typeof variableTyple); // returns object as string format

let variableAny: any = 4;

console.log(typeof variableAny); // returns number as string format

let variableUndefined: undefined = undefined;

console.log(typeof variableUndefined); // returns undefined as string format

enum Monday { Sunday, Monday, Tuesday }

let enumVariable: Monday = Monday.Sunday;

console.log(typeof enumVariable); // returns number as string format

var myFunction = new Function(‘123 + 6’);

console.log(typeof myFunction) // outputs ‘Function’

var currentDate = new Date();

console.log(typeof currentDate) // outputs ‘object’

Learn instanceof operator 

instaneof operator checks if the object is another object type and returns true. This checking is done at runtime. This will be checked in conditional e

Syntax 

name instanceof type

the name is of object name and type is of object type

instanceOf Examples

In the below code, date object name checks against Class and returns.

any variable returns false against date object

var todaysDate = new Date();

var variableNumber: any = 12;

console.log (todaysDate instanceof Date) // returns true

console.log (variableNumber instanceof Date) // returns false

instanceof class example

if undefined type checked with class, return false

class ClassExample {}

let classes: Array = [];

console.log(classes[0]); // returns undefined

console.log(classes[0] instanceof ClassExample);

//returns false  undefiend is not of ClassExample

classes.push(new ClassExample());

console.log(classes[0]); // outpus empty class

console.log(classes[0] instanceof ClassExample); // true

instanceof interface example

This is an example usage of the interface with instanceof operator

interface InterfaceExample { }

var interfaceVariable: InterfaceExample = { };

console.log(interfaceVariable instanceof InterfaceExample); // compilation error

class ClassExample { }

var variableClass: ClassExample = { };

console.log(variableClass instanceof ClassExample); // reutnrs false and no compilation error

as you see interface gives ‘InterfaceExample’ only refers to a type, but is being used as a value here error

Comments:

estt

Kiranbabu - Jul 2, 2018

estt

Similar Posts