ADVERTISEMENT
ADVERTISEMENT

Can (a== 1 && a ==2 && a==3) ever evaluate to true?

The solution for "can (a== 1 && a ==2 && a==3) ever evaluate to true".
Submitted by Pratishtha Saxena, on May 24, 2022

There's given a condition that if the expression is (a==1 && a==2 && a==3), then will this ever be true? Let's discuss this below.

So, the answer to this question is 'yes'. But you must be wondering how?

Before starting, you must have a basic knowledge of loose equality operator. Basically, it is nothing but the use of (==) to compare the values. This compares the value by first making the data types of the variables same and then comparing them.

Now, to make the expression true, we'll first create an object. Let's name that object 'a'. In this object, we'll declare a variable 'num' and initialize its value to 0.

Next, we'll create a function valueOf which will convert a to an int and increment it by one. So, the first time a is converted to int, it will return 1, then 2 and then 3. Which will hence satisfy the equation.

Let's make a clear picture of this by the implementing the following code.

Example:

const a = {
	num: 0,
	valueOf: function() {
		return this.num += 1
	}
};

const equality = (a == 1 && a == 2 && a == 3);
console.log(equality); // true

Output:

true

JavaScript Examples »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.