What is a function?
In general, a function is a block of code that we use to perform a specific task. In easier words, if we want to execute a piece of code n times with some slight changes, then instead of writing the code n times, we can simply put it inside a function and can call the function n times.
The syntax of a simple “Hello World” function would look something like this:
Whenever the user clicks on an element (could be a button), myFunction gets fired up, and we’ll see “Hello World” on the console.
Returning values from a function.
- Returning integer variable from the function.
- Returning string from the same function
How to use parameters in functions?
Parameters are the variables passed to the function or the variable which gets defined while declaring a function. Parameters variables have their access limited to the scope of the function.
Function parameters are highly useful for the cases where we have to pass a value to the function—for instance, calculating the cube of a number.
We have to assign an anonymous function to a variable to use in the future.
This function returns the character at the mentioned index in the string.
This function concatenates two strings into a single string.
Returns the length of a string.
Strikes through the given string.
The “<strike>Hello World</strike>” in HTML means “
The Date functions return today’s date and time of calling the function.
Frequently Asked Questions
- JS supports object-oriented scripting.
- It is both front-end and back-end friendly.
This means that the function will get fired whenever we click on an element or bring the mouse over the element.
The function to display the current date is:
The function to display the current time is:
Functions are an essential part of a programming language. It makes coding things so much easier & also the code looks more compact and neat.