0

I'm trying to understand JS, doing it with documentation.

Variable inside/outside a function is quite difficult.

I would like to do a module to execute a code written in multiple areas of my code to finally write it only once :)

// GLOBAL VARIABLES
let adherent_type, profil1;

//SOMEWHERE I ASSIGN A VALUE IN ADHERENT_TYPE VARIABLE
adherent_type = "famille";

//THE FUNCTION IN THE MODULE I CREATED IN NODEJS
function profil(adherent_type,profil1) {

    if (adherent_type === "famille") {
        profil1 = "Particulier";
    }

    return profil1;
}

// CALL THE FUNCTION 
carte_adherent.profil(adherent_type,profil1);

The result: profil1 = undefined

Variable adherent_type is ok, my issue is on profil1.

It is not working when I don't put "profil1" in the () of the function too.

Thank you very much for your help.

UPDATE : this as been resolved by @Telman

It has been resolved by adding "profil1 =" to :

CALL THE FUNCTION profil1 = carte_adherent.profil(adherent_type,profil1);

8
  • What is carte_adherent Commented Aug 2, 2022 at 0:17
  • First of all, there are no global variables here, only local variables of wider or narrower scope. Inside profil, profil1 is a parameter, which is just a local variable initialised from the calling argument. It is shadowing the outer variable profil1. Assigning to the inner profil1 inside profil thus has no impact on the outer profil1. If profil1 is not a parameter, there will be no shadowing, and profil1 inside profil will refer to the outer scope profil1, resulting in profil1 == "Particulier". Commented Aug 2, 2022 at 0:21
  • @JaxonCrosmas This is incorrect. The code is equivalent to let x = 3; function a(x) { x = 7; }; a(x); console.log(x) (producing 3, not 7, because of shadowing). OP's function will never result in anything other than undefined in profil1, regardless of if the function is called, or what the value of adherent_type is. Commented Aug 2, 2022 at 0:32
  • @Amadan My bad I misunderstood the question. Yes, you are correct. profil1 will always be undefined in this case. I was stating that the function profil is returning the expected value. But I guess OP is stating that they are evaluating profil1 after the function has completed, not evaluating the result of the profil function. Commented Aug 2, 2022 at 0:40
  • carte_adherent is the module name I created in nodejs. Commented Aug 2, 2022 at 13:21

2 Answers 2

1

When you pass a variable to the function, the function creates a copy of it. So when you change the profil1 inside the function the value of the let profil1 is not changed (only the copy is changed).

What you can do is to return the value of profil from the function, and then assign the result to the profil1 variable.

function profil(adherent_type) {
    let newProfil;

    if (adherent_type === "famille") {
        newProfil = "Particulier";
    }

    return newProfil;
}

// CALL THE FUNCTION 
profil1 = carte_adherent.profil(adherent_type);

The option with globalThis probably also will work, in a case you want to make the profil1 variable global:

   function profil(adherent_type) {
        if (adherent_type === "famille") {
            globalThis.profil1 = "Particulier";
        }
    }
    
    // CALL THE FUNCTION 
    carte_adherent.profil(adherent_type);
    console.log(globalThis.profil1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @telman :) It works. My error was on calling function, I missed the "profil1 =". It was more a logic error ;)
0

you don't call a function this way in javascript

  carte_adherent.profil(adherent_type,profil1);

instead this

  profil(adherent_type,profil1);

now to see your result console it to the machine like this

  console.log(profil(adherent_type,profil1));

now try it an see the result but first make the changes above

3 Comments

This isn't necessarily true. profil may be a function defined in an object named carte_adherent. In this case, the snippet makes sense. But OP has not provided this information.
Yes it is a nodejs module I create. I thought it was sufficient to tell nodejs module. sorry. As it is a module you put modulename.functionname to call it
yes I did a console.log and the result is profil1 as the value of "undefined"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.