why number 1 works as I need? why number 2 can't produce my desired result? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why number 1 works as I need? why number 2 can't produce my desired result?

Number 1: <script> function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script> Number 2: <script> function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display = "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script>

14th Jul 2018, 8:55 AM
Nayem
6 Answers
+ 4
If you want to compare always use == or ===. Simple example : var a=1 a=3 // will assign new value. if(a==3) // will check if a is equal to 3 or not {} Hope this helps☺️☺️.
14th Jul 2018, 9:11 AM
Meet Mehta
Meet Mehta - avatar
+ 1
Because a single "=" is the assignment operator. To compare values you need "==" The tripple "===" additionally checks whether the variables are of the same data type
14th Jul 2018, 9:07 AM
Matthias
Matthias - avatar
0
So in the 2nd method it sets the value of display to none. but in the first method it asked to check is the value of display is none? Am I right?
14th Jul 2018, 9:39 AM
Nayem
0
Yes, that's correct 👍
14th Jul 2018, 9:41 AM
Matthias
Matthias - avatar
0
thanks
14th Jul 2018, 9:41 AM
Nayem
0
Also take care -- Id has to be unique - there can be only one of them. you are using .getElementById and in two different cases you are referring to the same id. getElementById will only find the first occurance of "myDIV".
14th Jul 2018, 12:51 PM
Louis
Louis - avatar