Search code examples
javascriptparametersarguments

How does a compiler treats an argument value if it has two values? How does the compiler decide which one to take?


If I have variable x=10 and x=12;

and I passed a parameter function value(num);

and passed an argument .... value(x); to the parameter.

how does the execution works .. will the compiler checks 10<12 or directly implements 12 is the x value in the argument.


Solution

  • As mentioned in comments. There can't be two variables with the same name and different values in the same scope. The latest assigned value will be used:

    (() => {
      let x = 10;
    
      x= 12
    
      console.info(x);
    })();
    

    output : 12

    https://jsfiddle.net/uv6n3hs2/