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 ...