Default methods for variables in typescript

Author: Dawid Adach

-

As we learned in a previous lesson, TypesScript allows us to use static typing and use different type of variables. Each variable defines certain set of functions which can be used along with them. For instance:

  1. Declare following variables in app.components.ts:
  2.   let a: number;                    //numeric type
      let b: string;                    //string 
      let c: boolean;                   //true or false
      let d: any;                       //any (dynamic)
      let e: number[] = [1,2,3];        //array of numbers
      let f: string[] = ['a','b','c'];  //array of strings
      let g: any[] = [true, 1, 'a'];    //array of any 
      
  3. In a new line, type each of the variable, followed by dot (ie. a.) and observe result:
  4. Any type

    As you can notice, Visual Studio suggest function which can be used along with each variable type.

Numeric type methods:

# Method Description Result
1 a = 5.56789; Basic assignment 5.56789
2 a.toExponential() Convert a number into an exponential notation: 5.56789e+0
3a a.toFixed() Converts a number to a string keeping a specified number of decimals. 6
3b a.toFixed(2) 5.57
4 a.toLocaleString('ar-EG') Returns a string with a language sensitive representation of this number. ٥٫٥٦٨
5 a.toPrecision(2) Returns a string representing the Number object to the specified precision.
While toFixed(n) provides n length after the decimal point; toPrecision(x) provides x total length.
5.6
6 a.toString() Returns a string representing the Number object. 5.56789
7 a.valueOf() The valueOf() method returns the wrapped primitive value of a Number object. 5.56789

String type methods:

Warning

String prototype in TypeScripts offer more default methods than described below, however, they are marked as deprecated. That means, that they will be removed in the future so you should avoid using them. Some browsers may still support them, though.

# Method Description Result
1 b = 'Hello MDB World'; Basic assignment Hello MDB World
2 b.charAt(6) The String object's charAt() method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string. M
3a b.charCodeAt(6) The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index. 77
3b b.concat('. It is nice to meet you.'); The concat() method combines the text of one or more strings and returns a new string. Hello MDB Wolrd. It is nice to meet you.
4 b.endsWith('d') The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate. true
5.1 b.includes('B'); The includes() method determines whether one string may be found within another string, returning true or false as appropriate. true
5.2 b.includes('b'); false
6 a.indexOf('M') The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found. 6
7 a.lastIndexOf('l') The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found. 12
Coming soon:
  • b.localeCompare()
  • b.match()
  • b.normalize()
  • b.padEnd()
  • b.padStart()
  • b.repeat()
  • b.replace()
  • b.search()
  • b.slice()
  • b.split()
  • b.startsWith()
  • b.substr()
  • b.substring()
  • b.toLocaleLowerCase()
  • b.toLocaleUpperCase()
  • b.toLowerCase()
  • b.toString()
  • b.toUpperCase()
  • b.trim()
  • b.trimLeft()
  • b.trimRight()
  • b.valueOf()
  • b.[@@iterator]()
  • b.raw()

Any and boolean

Any type doesn't offer any predefined methods. Boolean offers to basic - toString which returns string of either 'true' or 'false, as well as valueOf which returns the primitive value of the Boolean object.


Next lesson

Spread the word:
Do you need help?: Use our support forum

About author

Dawid Adach
For more than 5 years Dawid was working as an IT Consultant specializing in SOA/EAI/ESB for banking domain. He was gaining experience working in countries like Netherlands, Belgium, Poland and India developing enterprise-class systems for the biggest companies within a domain. Since 2 years as a co-founder of mdbootstrap.com & brandflow.net is using and teaching others technologies like Angular, TypeScript, PHP, AJAX, Mongo, SQL, Hadoop Stack, Virtualization, Automation and many others...
Sign up to follow your progress and get additional benefits