Javascript Failing to Correctly Compare Two Strings -


i first create list of characters, , save 2 of them strings dependent on user input. unsure issue lies copied full code here https://jsfiddle.net/mboguslaw97/6w6xj1f3/3/. top left card , 1 2 below both 'l's. click card after activate alert statements.

var letters = 'abcdefghijkl'.repeat(2).split(''); user_input = 11; cards_flipped.push(user_input); user_input = 23; cards_flipped.push(user_input); letter1 = letters[cards_flipped[0]]; letter2 = letters[cards_flipped[1]];  alert(letter1 == 'l'); alert(letter2 == 'l'); alert(letter1 == letter2) //alerts true, true, false 

could please explain me how detect if strings equal , why strings cannot compare way?

console.log(typeof letter1, letter1 instanceof array); object true 

letter1 , letter2 both single value arrays. reason either of them "equals" letter 'l' because of type conversion: tostring() method being called on array when comparing string literal. if compare letter1.tostring() == letter2.tostring() or letter1[0] == letter2[0], equal. updated fiddle.

cheers.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -