I need some help Idk why the code isn't working

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

function functionWithArgs(a, b) {    
console.log(a + b);
 }
 functionWithArgs(1, 2);

 function functionWithArgs(a, b) {
   console.log(a + b);
 }
 functionWithArgs(7, 9)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Passing Values to Functions with Arguments

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

You cannot declare or define a function more than once in the same scope.

that was the code I followed could tell me a ilttle bit more

Well, you can. Just not in the challenge.

Looks like it is Babel throwing the error.


@felizseguraabdias Just don’t have the function definition twice.

This is a function definition:

this is another one for the exact same function:

You can only have 1 function definition, not 2

Can you in non-strict mode? I sort assume strict as the default mode for writing JS? Accidental function redefinition is definitely the sort of bad behavior that should not be allowed.

SyntaxError: unknown: Identifier 'functionWithArgs' has already been declared. (6:10)

Really, I suppose you can re-define but not-redeclare in strict mode since a function is secretly a variable.

No, I think it is specific to Babel.


This is how it would normally work.

'use strict'

function functionWithArgs(a, b) {
  console.log(a + b);
}

functionWithArgs(1, 2);

function functionWithArgs(a, b) {
  console.log(a + b); // 3, 16
}

functionWithArgs(7, 9);

The function definition is overwritten. Then the function is hoisted so the first function call that comes before the second function definition still calls the second version of the function.

That’s aweful! Most languages ban that sort of bugginess.

It is JavaScript, anything goes. :stuck_out_tongue:

But I agree, it isn’t the behavior you really want. That is one benefit of using function expressions with let/const for your definitions, so you can’t redeclare them by accident.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.