Function calling

https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/arrow-functions

So I am not quite sure when we assign a function to a variable as cons, if these work In Js as variables or as functions when called?

function colorRed() {
  document.body.style.backgroundColor = "red";
}

const colorRed = function() {
  document.body.style.backgroundColor = "red";
}

Whether you create a named function (top example) or you store an anonymous function in a variable (2nd example), they’re both essentially giving us a name we can use to call the function when we want.

In both cases we can use this to call it. JavaScript uses () to execute a function.

colorRed()

We can also make the 2nd example into an anonymous arrow function which we store in a variable…it does the same thing and can be called in the same way with colorRed()

const colorRed = () => {
  document.body.style.backgroundColor = "red";
}
2 Likes

Hi Steve, thanks! my doubt was because on Introduction to Javascript every function right now it is being used on a log method (I hope I said that right), on a console.log, so when I assign a function through the fist example, console.log was already on the block, nevertheless on the second example console.log wasn´t and I could not see anything happening just by calling the variable

If the console.log method is in the code block (the part in between { }) it should work the same no matter how you set up the function.

const secretMsg = () => {
  console.log("Whisper whisper whisper...");
}

const secretMsg = function() {
  console.log("Whisper whisper whisper...");
}

function secretMsg() {
  console.log("Whisper whisper whisper...");
}
1 Like

Thanks Steve! Could you help me with another function doubt?
https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/return

In this lesson these lines of code kind of troubles me
function rectangleArea(width, height) {
let area = width * height
}
console.log(rectangleArea(5, 7))

then, to avoid getting undefined on the log, we need to use a return, nevertheless I don´t understand why I need to hoist a variable inside the function when I can write these instead:

function rectangleArea(width, height) {
width * height;}

Could you help me understand the point of hoisting these variables

You have to return something or the function won’t return a value. it will do the calculation, store it in the variable area…but then no output…because nothing is being returned. In other words, the function will not output the value of width * height.

So just add that keyword ‘return’

function rectangleArea(width, height) {
 return width * height;
}

// With storing the value in a variable
function rectangleArea(width, height) {
  let area =  width * height;
  return area;
}

The function takes two inputs: width and height. In order to get the resultant output you have to use the keyword return.

3 Likes

Thanks!, already tried it and I am kind of getting it

1 Like