4

I am trying to learn conditional statements in JavaScript, when I called the function with passing no argument still I am getting the x is equal to y. I don't understand where I m missing the code.

function tryMe(x, y) {
  if (x == y) {
    console.log("x and y are equal");
  } else if (x > y) {
    console.log("x is greater than y");
  } else if (x < y) {
    console.log("x is less than y")
  } else {
    console.log("no values")
  }
}

tryMe();

This is my console log:

x and y are equal // i am expecting it to console.log("no values")

Mustkeem K
  • 8,158
  • 2
  • 32
  • 43
Affy
  • 181
  • 1
  • 2
  • 13

7 Answers7

6

Because undefined is equals to undefined

When you don't pass params, it get undefined both x and y

Why that happens - When you just declare a variable it's have default value undefined. Which is same happens in your case, your fn tryMe() declared x and y which has default value undefined and when you compare them then both are equal.

console.log(undefined == undefined)

var x, y
// Here you declared the variable which happens in your function
if(x === y) {
  console.log('You never defined what value I have so Javascript engine put undefined by default')
}
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
6

This happens because when you call tryMe(), both x and y are undefined, meaning they are equal. So, you will need to check if there is value assigned to x and y first.

function tryMe(x, y) {
  if (typeof(x) != 'undefined' && typeof(y) != 'undefined') {
    if (x == y) {
      console.log("x and y are equal");
    } else if (x > y) {
      console.log("x is greater than y");
    } else if (x < y) {
      console.log("x is less than y")
    } else {
      console.log("no values")
    }
  } else {
    console.log("no values")
  }
}

tryMe();
tryMe(1);
tryMe(1, 2);
Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48
4

When no arguments are passed in x = undefined and y= undefined

x == y // true
andrew
  • 9,313
  • 7
  • 30
  • 61
4

if you don't pass in any arguments to your function the x and y and initialized with undefined and undefined === undefined that's why you're getting x and y are equal

a better way to do this will be

function tryMe(x, y) {
  if(!x || !y) return console.log('No values')
  if (x == y) {
    console.log("x and y are equal");
  } else if (x > y) {
    console.log("x is greater than y");
  } else if (x < y) {
    console.log("x is less than y")
  }
}

tryMe()
Prithwee Das
  • 4,628
  • 2
  • 16
  • 28
3

As you are not passing any argument in the function call, functions parameters x and y both are undefined, thus they are equal:

function tryMe(x, y){
  console.log(x == y); // true
  if (x == y){
    console.log("x and y are equal");
  } else if(x > y){
    console.log("x is greater than y");
  } else if (x < y){
    console.log("x is less than y")
  } else {
    console.log("no values")
  }
}

tryMe();
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • This answer was a bit confusing for me, even if absolutely correct. But the highlighted words "argument" and "parameter" confused me because they are bold. Answer would be less confusing if those words are not written in bold letters. Moreover, even if you are right that there _is_ a difference between _argument_ and _parameter_, the consense is that the difference is minor, so that it is valid to interchange the terms in most situations: https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter – Christoph Bimminger Jul 13 '19 at 19:55
  • @ChristophBimminger, *Parameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function*...... I simply highlighted the terms to differentiate them from one another.......thanks:) – Mamun Jul 14 '19 at 03:53
2

You Can throw error when param is required like:

    const isRequired = () => {
      throw new Error('param is required'); 
    };
    function tryMe(x = isRequired() ,y = isRequired() ){
             if (x == y){
            console.log("x and y are equal");
        } else if(x > y){
            console.log("x is greater than y");
        } else if (x < y){
            console.log("x is less than y")
        } else {
            console.log("no values")
        }
    }
    tryMe(2,3);
    tryMe(2);
    tryMe();
It will make your params mandatory.
or you can do this:

    function tryMe(x ,y ){
        if(x == "" || x == undefined || y == "" || y == undefined){
          console.log("no values !");
        } else if (x == y){
            console.log("x and y are equal");
        } else if(x > y){
            console.log("x is greater than y");
        } else if (x < y){
            console.log("x is less than y")
        } else {
            console.log("no values")
        }
    }
    tryMe(2,3);
    tryMe(2);
    tryMe();
Muhammad Bilal
  • 497
  • 1
  • 5
  • 12
1

Both x and y end up being the undefined value, which naturally compares as equal.

AKX
  • 152,115
  • 15
  • 115
  • 172