6. It really doesn’t care
Semicolons are optional
Parameters may or may not be passed
7. It really doesn’t care
function getMessage(message) {
internalVariable = “The message is: ”
return internalVariable + message
}
var result = getMessage(“dogs are nice”, 123, undefined, “etc”)
console.log( result )
// this’d positively print “The message is: dogs are nice” without errors
16. Type undefined
People say that undefined may change the value. I don’t care about that.
if (newVariable === undefined)
This is perfectly fine for my standards.
(function(undefined) {
console.log(undefined); // Prints the given string, my own undefined
})(“my own undefined”);
This is stupid. So I don’t care about undefined not being undefined.
17. Type null
Very common value in other languages.
Can be seen a lot retrieving data from APIs.
var received = null;
if (received === null) {} // True
if (!received) {} // True too
18. Type boolean
var that = true;
if (that === true) {} // This is silly
if (that) {} // This is better
if (that == false) {} // Same as the first, silly
if (!that) {} // This is great
19. Type string
var text = “There was a time”;
if (text != “”)
console.log(“Text is not empty”);
// Above code is far too silly, it can be simpler as below
if (text)
console.log(“Text is not empty”);
20. Type number
var luckNumber = 5;
if (luckNumber != 0)
console.log(“Number is not zero”);
// This can be easier:
if (luckNumber)
console.log(“Number is positive or negative, not zero”);
21. Type object
// Right way to initialize an object, not using new
var virtualObject = {};
if (virtualObject)
console.log(“Object exist”); // Even without attributes, it
exist
virtualObject.firstAttribute = 1234; // Add attribute
if (virtualObject.unexistingMethod) // Continue only if method available
virtualObject.unexistingMethod(); // Unknown method error prevented
22. Type object
var dogs = [];
if (dogs) // Object
actually exist
dogs.push(“Terry”); // Now has one
element
console.log(dogs); // Prints [“Terry”]
If (dogs.length) // Length won’t be
zero
console.log(“Array has elements”); // So this actually prints
25. Conditionals
if (typeof aName == “string” && aName != “”) {
console.log(“Say hello to ” + aName);
}
This is wrong, javascript has a conditional of 3 equals.
Do NEVER use the 2 equals.
2 sometimes are OK, 3 will be always OK. Use 3 equals always.
26. Conditionals
if (typeof aName === “string” && aName !== “”) {
console.log(“Say hello to ” + aName);
}
Now this is still pretty awful. If aName is integer 18 then you cannot print hello to
18.
27. Conditionals
if (aName !== “”) {
console.log(“Say hello to ” + aName);
}
Yet horrible, there is no need to ask for not empty string explicitly
when it can be simpler.
28. Conditionals
// From `if (typeof aName == “string” && aName != “”)`
// To
if (aName) {
console.log(“Say hello to ” + aName);
}
Simplicity is on our side. Take that [INSERT CORRECT STARS
WAR CHARACTER]!
30. Types Conversion
Now we are entering to the fun side
JavaScript is well known for its types conversion table
Also for its logic tables
And for everything else, but not in the good way
More as a joke
So lets try to get a good laugh from it
46. Nested Scopes
JavaScripts starts at the currently executing scope, looks for the
variable there, then if not found, keeps going up one level, and so
on. If the outermost global scope is reached, the search stops,
whether it finds the variable or not.