最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Conditional statements with no arguments - Stack Overflow

programmeradmin5浏览0评论

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();

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")

Share Improve this question edited Jul 10, 2019 at 5:06 Mustkeem K 8,7682 gold badges34 silver badges44 bronze badges asked Jul 10, 2019 at 5:02 AffyAffy 1891 gold badge2 silver badges14 bronze badges 2
  • 3 undefined==undefined – Shubham Dixit Commented Jul 10, 2019 at 5:06
  • when calling your function as tryMe(); is equal to tryMe(undefined, undefined); – Azad Commented Jul 10, 2019 at 5:09
Add a comment  | 

7 Answers 7

Reset to default 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);

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')
}

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

x == y // true

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()

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();

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();

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

发布评论

评论列表(0)

  1. 暂无评论