Javascript and weakly defined types (Part 2: Electric Boogaloo)

Javascript and its lack of strong typing continues to cause headaches.
Today's one is caused by the following bit of madness

if (true) {
    alert("true evaluates as true"); // will show
}

if (false) {
    alert("false evaluates as true"); // will not show
}


if ('true') {
    alert("'true' evaluates as true"); // will show
}

if ('false') {
    alert("'false' evaluates as true"); // will show
}

Any string that is not an empty string is treated as a true value automatically.

Be careful that any method or property that returns a true/false value returns it as a boolean value and not as a string


Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to SimonReynolds.ie
Great! Next, complete checkout for full access to SimonReynolds.ie
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.