pq the function add this being interpreted before your scope statement?

Asked

Viewed 65 times

2

While testing some Javascript features in learning I came across the following problem, when executing this code I could not understand why the addition function did not trigger an error and it was only declared well after being called in the code.

console.log(somar(5,5))        //<--copila
console.log(mult(5,5))         //<--nao copila

function somar(x,y){
   return x +y
}

const mult = function (x,y) {
   return x * y
}

2 answers

6


This "logical order" makes no difference to Javascript due to the behavior of Hoisting of the language that allows calling a variable or function before declaring it.

However, your problem is you’re calling a const before you even start it and Javascript does not allow it. A const, first you initializes and then access.

Just reverse the order by accessing the const after your statement:

    const mult = function (x,y) {
       return x * y
    }

    console.log(mult(5,5))

2

you are declaring "mult" as const and must be a Function to receive the parameters. Declare it as the sum Function

console.log(somar(5,5))        
console.log(mult(5,5))

 function somar(x,y){
   return x +y
}

function mult(x,y) {
   return x * y
}
  • So, what I did was define a const called mult and assign to it an anonymity function only that all this occurred well after calling it so the error occurred, but I did the msm with the sum function and that error n appeared, was that q n manage to understand ;s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.