Built-in methods are called, or used, by appending an instance with a period, the name of the method, and opening (() and closing ()) parentheses. Consider the examples below:
console.log('Hello'.toUpperCase()); // 'HELLO' console.log('Hey'.startsWith('H')); // true
Let’s look at each line separately:
- On the first line, the .toUpperCase() method is called on the string instance ‘Hello‘. The result is logged to the console. This method returns a string in all capital letters: ‘HELLO‘.
- On the second line, the .startsWith() method is called on the string instance “Hey”. This method also accepts the character ‘H’ as an input between the opening and closing parentheses. Since the string ‘Hey’ does start with the letter ‘H’, the method returns the boolean true.
Play with the code below changing text values and notice the outputs. Remember the more you code the more you know.