Functions in Javascript

Página 6

Functions are an essential part of JavaScript as they allow the developer to create reusable and organized blocks of code. A function is a block of code that can be called from anywhere in the code, allowing the developer to perform the same task multiple times without having to repeat the same code over and over again.

To create a function in Javascript, it is necessary to use the keyword "function", followed by the name of the function and its parameters, if any. For example:


function sum(a, b) {
  return a + b;
}

In this example, we create a function called "sum" that takes two parameters, "a" and "b". Inside the function, we use the sum operator to add the two parameters and then use the "return" keyword to return the result of the sum.

To call the function, just use the name of the function followed by the parameters in parentheses. For example:


var result = sum(2, 3);
console.log(result); // 5

In this example, we call the "sum" function passing parameters 2 and 3. The result of the sum is stored in the "result" variable and then displayed on the console.

In addition, functions in Javascript can be anonymous, that is, they do not have a defined name. For example:


var sum = function(a, b) {
  return a + b;
}

In this example, we create an anonymous function that takes two parameters, "a" and "b". Inside the function, we use the sum operator to add the two parameters and then use the "return" keyword to return the result of the sum. The function is stored in the "sum" variable.

Functions can also be passed as parameters to other functions, which is known as a "callback". For example:


function executeFunction(function, a, b) {
  return function(a, b);
}

var result = executeFunction(sum, 2, 3);
console.log(result); // 5

In this example, we create a function called "executarFuncao" that receives three parameters: a function, "a" and "b". Inside the function, we call the function passed as a parameter, passing the parameters "a" and "b". The result of the function is returned.

Finally, functions in Javascript can be used to create objects, known as "constructor functions". For example:


function Person(name, age) {
  this.name = name;
  this.idade = age;
}

var person1 = new Person("John", 30);
console.log(person1.name); // John
console.log(person1.age); // 30

In this example, we create a constructor function called "Person" that takes two parameters, "name" and "age". Inside the function, we use the keyword "this" to create the "name" and "age" properties of the object. Next, we create a new object using the "new" keyword and the constructor function. The object is stored in the variable "person1" and then its properties are displayed in the console.

Now answer the exercise about the content:

_What is the purpose of functions in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

7Objects in Javascript

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text