


Rather than writing this logic out multiple times for each link, you could abstract it away into a function. When a user clicks on one of the links, you need to update the URL in the browser’s address bar and load the new page. The navigation bar has a few different links, each of which leads to a different page on the site. However, a good rule of thumb is to call functions when you need to execute a certain block of code multiple times or when you need to perform a task that can be abstracted away into a function.įor example, let’s say you have a website with a navigation bar. It depends on how you’ve structured your code and what you’re trying to achieve. There really isn’t a definitive answer to this question. When Should You Call a Function in Javascript? This is a very simple example, but you can use this same syntax to call functions with more complex logic.

MyFunction('Hello, world!') // prints 'Hello, world!' to the consoleĪs you can see, we just pass in the message that we want to print as an argument to our function.
