What value does the if () of this section of JavaScript take, true or false?

3

I'm new to JS and I want to know what value the if (drawer.style.left) takes, at the beginning it's worth -30% (which I think is false) so it goes to the else, and then when I press again enter the if, but I do not understand why if it is now worth -10% and I think it is false.

Thank you very much.

		let menu = document.querySelector(".material-icons");
		let drawer = document.querySelector("#drawer");
		menu.addEventListener('click',function(e){
			if(drawer.style.left){
				drawer.style.left = '';
				drawer.style.transition = 'all 0.8s';


			}
			else
			{
				drawer.style.left = '-10%';
				drawer.style.transition = 'all 0.8s';
			}

		});
    
asked by Juan Franco Narváez 28.08.2018 в 01:20
source

1 answer

2

The .style property in javascript only works with styles that have been declared online. Also, the properties in css are of type string, so any property that is not empty will return true.

document.addEventListener("DOMContentLoaded", function(event) {     
    let menu = document.querySelector(".material-icons");
		let drawer = document.querySelector("#drawer");
		menu.addEventListener('click',function(e){
      console.log(drawer.style.left);
			if(drawer.style.left){
				drawer.style.left = '';
				drawer.style.transition = 'all 0.8s';
        console.log(true);
			}
			else
			{
				drawer.style.left = '-10%';
				drawer.style.transition = 'all 0.8s';
        console.log(false);
			}

		});
});
<div id="drawer" style="left: -30%">Test</div>
<button class="material-icons">Icons</button>
    
answered by 28.08.2018 в 01:46