Jump to content

JavaScript if function error


Bebo Khouja
 Share

Recommended Posts

So i have a code in my JavaScript but however it does not hide the text. Here is the code:

 

var isDebug = "false";
if (isDebug = true) {
var x = document.createElement("A");
  var t = document.createTextNode("Debug is on");
  x.appendChild(t);
  document.body.appendChild(x);
} else {
	false
}

 

The HTML still says "Debug is on" to anyone. I restricted to developer only to see this

Link to comment
Share on other sites

Your first problem is that you are assigning the string "false" to the variable. Strings are always true unless they are empty. Remove the quotes if you want to use the boolean value false.

var isDebug = false;

The second problem is that, to compare values, you have to use the == operator. A single = will change the value of the variable.

if (isDebug == true) {

The content in your else statement is not doing anything. It doesn't make sense to just type a value in a line of code. I'm not sure what you intended to do by just writing false, so I can't replace it with anything.

A working version of the code would look like this:

var isDebug = false;
if (isDebug == true) {
var x = document.createElement("A");
  var t = document.createTextNode("Debug is on");
  x.appendChild(t);
  document.body.appendChild(x);
}

I left the "== true" in to show how the == operator works, but it is not necessary when testing boolean values and could be shortened to

if (isDebug) {
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...