👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
JavaScript functions are custom blocks of code that can be reused. The functions allow the code to be more modular and are essential for object oriented programming. The functions can be defined by statements or expressions.
If you are looking to learn JavaScript and are looking to know the basics, JS functions are definitely something you have need to know. If you’ve spent time working with a programming language, you will know that functions are the building blocks of complex programs, but you may not know how they work. Functions allow you to write code once for common processes, instead of repeating it multiple times.
Functions are blocks of code that perform an action and can return a value. The functions can be customized to your needs and can be used to make your code more efficient and modular
In this tutorial, we will explore the basic functions:. How to define a function, how to call a function and when they can be useful.
How to use JavaScript define function
There are two ways to define a JavaScript function: through declarations and expressions. Let’s start with the declaration method to define a function.
JavaScript function declarations
function declarations defines a called function. To define this type of function, you must start your code with the function keyword , followed by the name of the function. Here is an example:
Function names follow the same rules as variables: they can use, underscores, leers numbers and are often written in camel cases. Then, after the variable name , you can include a series of parentheses, in which optional parameters can be kept. We will come back to this later in the article.
Then, like an or for if statement, the function code will be in braces. Here is an example of a function that will print Google
to the console: