/    Sign up×
Community /Pin to ProfileBookmark

Strange question, any answers?

I have just been talking to a fellow developer and he came accross a JavaScript if statement that goes something like this:

if(“j” == ‘j’) {
document.write(someText);
} else {
writeText(someText);
}

function writeText(text) {
document.write(text);
}

My question is, what would fail at testing the j in double quotes to single quotes? Something that doesn’t like using document.write unless it is from inside a function?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@DokAug 15.2007 — if("j" == 'j') is always true. Using double or single quotes doesn't matter - it still creates a String so if("j" == 'j') is always true and if("j" === 'j') is always identical.
Copy linkTweet thisAlerts:
@web_bertauthorAug 15.2007 — Thanks for the reply Dok, I realise that it will always return true. So my question is why would someone do a check for this? There must be some kind of browser that would fail with this check?

It is my understanding that if this was a Java statement, the double quotes would make it a string and the single would make it a char, so this would fail. It may have just been a Java programmer forgetting that he was using JavaScript?? But just thought I would see if there is any other explanation for this?
Copy linkTweet thisAlerts:
@Angry_Black_ManAug 15.2007 — nope. the buck stops with dok. id entertain your suggestion, however.
×

Success!

Help @web_bert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...