Practice Set - Truth, Equality & JavaScript

by subsari

JavaScript

function compareCoercionDoesntWork(){
	var a = 6;
	var b = "ten"; 
	
	if (a < b || b < a) {
		alert("this is true!");
	}
}

function compareCoercionDoesntWorkExplained(){
	var a = 6;
	var b = "ten";
	
	b.prototype.valueOf = function(){
		console.log("running valueOf");
		console.log(this);
		return 10;
	};
	
	if (a < b || b < a) {
		alert("this is true!");
	}
}

function compareCoercionDoesWork(){
	var a = 6;
	var b = "10";
	
	if (a < b || b < a) {
		alert("[function compareCoercionDoesWork()] this is true!");
	}
}


//compareCoercionDoesntWork();
compareCoercionDoesntWorkExplained();
//compareCoercionDoesWork();