Why does only half of my if / else statement work?

I’m really new to javascript so i decided to just make a simple hide / show script for a div. the test worked and i went on to try and implement it on the site that i’m building.

the problem is that only the hide function fires, the show function does not get an error message but i tried running the function in console and it said variable (wrapper) undefined. i understand the problem but not how to fix it. here is the code and what i want to know is why won’t the variable (wrapper) stay defined ?
again, it hides the div, then doesn’t work anymore. when i try to execute the else function on its own in the browser i have to first define what wrapper is. the first time i clicked the if statement used wrapper so it was defined then, help please!?

Backpack.onclick = function(){
var wrapper = document.getElementById (“home”);
var display = window.getComputedStyle(wrapper, null).getPropertyValue(“display”);

				if(display = "block"){
					wrapper.style.display = "none";
				}
				else {
					wrapper.style.display = "block";
				}
		}

= is an assignment, not a comparison.

Thank you for replying, but as i said i’m new to javascript and even though i understand your words i don’t quite understand what you mean? would you care to explain it? please.

if (display = "block") will always evaluate as true since you assign a non-empty string to display.

1 Like

Use == for comparison.

1 Like

even better, use ===.

2 Likes

thank you @Gandalf !! you solved my problem and i now understand what you mean. huge thanks!
@Dormilich, what is the difference between == and ===?

=== also checks the types, i.e. 0 === '' would return false (Number vs. String), while 0 == '' would return true.

2 Likes

you mean that === checks if number is true? or if string is true?

it checks that both values are of the same type.

1 Like

see also here for more information:

1 Like

I’m 100% sure that you are correct but again, my lack of understanding of the language so far prevents me from understanding =/ i hate that html and css came so easily to me but javascript is proving to be a real challenge in comparison. i hope i don’t take too long though, i need to learn PHP and MySQL too according to a friend of mine.
do you have any tips? i started learning and building a website last friday, in terms of looks i’m 80% there, so there’s not much more css and html to do and learn for that particular page, but in terms of functions for that page i still have an insane amount of ground to cover.

MDN also has a beginner guide and several tutorials to get you started.

1 Like

@dormilich I only saw it after i hit reply on my earlier post. Thank you =)!
I’ve watched the hell out of a lot of youtube videos about javascript but many of them are a bit too advanced for me even though they’re for beginners.
If you’re interested in what the page looks like (looked like 5 hours ago or smth like that) you can check out another post i made where i talk about a CSS problem that i still haven’t been able to solve. the entire css and html file is copied into there, its not much but its mine (except for the image url in the css file hehe)
i’ve really taken a liking to coding and it is so addictive, every time i finish one thing i tell myself “just one more fix, just that next button or list or link or image, just one more thing” i barely slept last night.

Interesting fact, if one operand in the (loose) equality comparison is a string and the other a number, then it’s always the string that will be converted to a number before comparing the values.

1 Like

well yeah, as i have understood it javascript does not deal in letters so strings would have to be assigned a numerical value, no?

wrong.

strings are strings, numbers are numbers, strings are not numbers (although you can convert many data types to string). how that is implemented internally, does not matter (although it is interesting to know how Unicode is implemented … cf. https://rainsoft.io/what-every-javascript-developer-should-know-about-unicode/).

the simple reason for the string => number conversion is that String(0) == '' is false but 0 == '' is (supposed to be) true.

1 Like

oh, cool. i think i get it! thanks :slight_smile: ! you’re awesome for bothering!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.