Readability is something you need to be aware of, but don't worry about learning bad practice. You'll become opinionated pretty quickly on your own.
So true.
Variable declarations and assignments happen on multiple lines in the 2nd snippet instead of all being defined at once.
var currentLanguage = "en";
var spanish = "es";
var german = "de";
var english = "en";
vs var currentLanguage = "en", spanish = "es", german = "de", english = "en";
Additionally, "en" is assigned twice. If you're going to take the time to assign the "en" abbreviation into the "english" variable, then you should use it when assigning to "currentLanguage", in my opinion, if only to avoid typos and redundancy. var spanish = "es", german = "de", english = "en", currentLanguage = english;
The "disclaimer" element is accessed using the documentGetElementById DOM call twice. javascript is getting faster, but forcing multiple DOM calls when it's not necessary is bad practice. Ideally, you'd access it once and save it into a reference. document.getElementById("disclaimer").firstChild.nodeValue = response;
var text = encodeURIComponent(document.getElementById("disclaimer").firstChild.nodeValue);
vs var disclaimer = document.getElementById("disclaimer"), text = disclaimer.firstChild.nodeValue;
disclaimer.firstChild.nodeValue = response;
The code formatting is poor and inconsistent, notice an incorrect space after "encodeURIComponent (", but no spaces after "getElementById(" and "getElementsByTagName(". Also notice no indentation anywhere, or line-breaks after the function signatures and opening brackets "{";The code is polluting the global namespace by not being enclosed in its own namespace or perhaps an immediately executed anonymous function.
I'm sure more seasoned JS devs can list some more things wrong with the code, but those are the ones that stood out to me.
var x = 0, y = 1, z = 0;
// Some looping or similar here
I do agree with your using of `english` variable to assign `currentLanguage`.Using a single var and putting each declaration on its line is not incompatible:
var foo = 4,
bar = 5,
baz = 6,
qux = 'quux; var foo = 4,
bar = 5,
baz = 6,
sprinkles = 'wee';
qux = 'quux';
Making `qux` global.There's nothing wrong with it, especially in JS where accidentally deleting a variable declaration can suddenly turn your variable global.
The problem with the code is more that the code stinks, even after you remove the formatting problems, it looks more like .Net code and it doesn't make any sense.
The formatting is non-existant. That makes me angry. Because I have OCD. Most programmers do.
There's a lot of direct use of DOM traversing, which makes the code rather brittle. (Use a css selector for binding to the DOM - jQuery is the de-facto standard tool here).
Variables are not encoded in the URL. And when it is, it happens on a separate line from where it's used. That's just bad style.
Oh, and what's the point of those declared-but-unused variables (`spanish, german, english`).
And why aren't they in a hashmap?
I assume you mean fake, self-diagnosed OCD, then.