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:
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 |x and y are equal // i am expecting it to console.log("no values")
7 Answers
Reset to default 6This 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.
tryMe();
is equal totryMe(undefined, undefined);
– Azad Commented Jul 10, 2019 at 5:09