Introduction to built-in JavaScript methods
We have seen many built-in JavaScript methods already. Any method that we didn't define ourselves is a built-in method. Some examples include console.log()
, Math.random()
, prompt()
, and many more—think about methods on arrays for example. The difference between a method and a function is that a function is defined anywhere in the script, and a method is defined inside a class. So methods are pretty much functions on classes and instances.
Methods can often be chained as well; this is only true for methods returning a result. The next method will then be performed on the result. So for example:
let s = "Hello";
console.log(
s.concat(" there!")
.toUpperCase()
.replace("THERE", "you")
.concat(" You're amazing!")
);
We create a variable, s
, and we store Hello
in there on the first line. Then we want to be logging something. This code has been divided over...