TypeScript comparison with JavaScript

Let's look at an example of how TypeScript makes life easy with types and error checking. Take a look at the following JavaScript code. It's perfectly legal code, though it is not following the recommended best practices:

function getLargestNumber(arr){    result=0;    for( index =0; index < arr.length; index++){              if(result < arr[index]){            result =  arr[index];        }    }    if(result > 0) {      result = true;    }    else result = false;        return result;}score = [1,2,3,4,5,6,];highestScore= getLargestNumber(score);

The preceding code, when executed, will return true from getLargestNumber, something which you would not have expected.

This code has multiple potential bugs in it:

  • The result, index, score, and highestscore variables  ...

Get TypeScript 2.x By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.