How to use Functions in JavaScript?

How to use Functions in JavaScript?

We are gonna take look at various ways/methods of using JavaScript Function. Functions are the fundamental part of JavaScript programming language. A function defined as the property of an object, is called a method to the object. A function designed to create new objects, is called an object constructor.

Functions are the fundamental part of JavaScript programming language. It is a block of code that is reusable, i.e. the function can be called anywhere in the code and perform the required task as many times as you need it.

In this article, we are gonna take look at various ways/methods of using JavaScript Function

  • Function statement:- In this method, we simply declared the function using the function keyword along with the name of the function. We passed the arguments (if any) in the round() bracket and the write the code what you want the function to perform when it is called or invoked inside the parenthesis {}.

In the function statement, the function gets hoisted, that is you can access or invoke the function even before it is declared.

square(4) //Output: 16

function square(a){
    console.log(a * a);
}

square(2)

//Output : 4
  • Function expressions:- An anonymous function is created, which does not have its name and therefore we create an expression by assigning to a variable using the equal = operator.
var square = function(a){
    console.log(a * a);
}

square(2)

//Output : 4

The square the variable is not the name of the function, it just points towards the address of the function in the memory space.

Note:- In function expression, we cannot access the function before it is declared as it will return the variable `square_` as `undefined_`

javascript web-development programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.