Skip to content

Instantly share code, notes, and snippets.

@Elbone
Last active August 17, 2017 00:13
Show Gist options
  • Save Elbone/08f9ef409af076acb373 to your computer and use it in GitHub Desktop.
Save Elbone/08f9ef409af076acb373 to your computer and use it in GitHub Desktop.
Ha ha javascript (gotchas and quirks)
console.log(",,," == new Array(4)); // true // A string is an array
console.log([] + []); // "" // Array plus array = empty string (OF COURSE!)
// Array + object
console.log([] + {}); // [object Object]
// now flip it
console.log({} + []); // 0 // What the hell?!?
// Object + object
console.log({} + {}); // NaN // Noooooo
console.log(Array(16)); // ,,,,,,,,,,,,,,,, // Empty array as commas
// Add/Subtract
console.log("Thing" + 1); // "Thing1"
console.log("Thing" - 1); // NaN
// http://www.smashingmagazine.com/2011/05/30/10-oddities-and-secrets-about-javascript/
console.log(typeof null); // object // Null is an object
console.log(typeof NaN); // 'Number' // NotANumber is a number?!!?
console.log((NaN === NaN); // false // NaN is not equal to itself
console.log(new Array() == false) // true
// JavaScript secretly coerces our variable into its truthy or falsy equivalent,
// which in the case of 0 is falsy.
var someVar = 0;
console.log(someVar == false); // true // zero is a falsy
console.log(someVar === false); // false // zero is a number, not a boolean
// Javascript is loosely typed, EXCEPT in switch statements.
// JavaScript is NOT loosely typed when it comes to case comparisons.
var myVar = 5;
if(myVar == '5'){ // true // since Javascript is loosely typed
alert("hi"); // this alert will show since JS doesn't usually care about data type.
}
switch(myVar){
case '5':
alert("hi"); // this alert will not show since the data types don't match
}
// Floating-point arithmetic
console.log(0.1 + 0.2); // 0.30000000000000004
// UNDEFINED CAN BE DEFINED
// Strange as it might sound, undefined is not actually a reserved word in JavaScript,
/// even though it has a special meaning and is the only way to determine whether a variable is undefined.
// So:
var someVar;
alert(someVar == undefined); // true
// So far, so normal. But:
undefined = "I'm not undefined!";
var someVar;
alert(someVar == undefined); // false!
// Functions and properties share the same namespace in JavaScript.
// So, when an id in your HTML has the same name as one of your functions or properties,
// you can get logic errors that are hard to track down.
var listitems = document.getElementsByTagName('li');
var liCount = listitems.length; // if you have <li id="length">, returns that <li> instead of a count.
// Uh oh
"" == 0 // true // empty string is coerced to Number 0.
0 == "0" // true // Number 0 is coerced to String "0"
"" == "0" // false // operands are both String so no coercion is done.
"3" - "1"; // 2 // coerces strings into numbers
// Types
typeof {} === "object" //true
typeof "" === "string" //true
typeof [] === "array"; //false
// The "+" Operator Both Adds and Concatenates
// http://www.codeproject.com/Articles/182416/A-Collection-of-JavaScript-Gotchas
1 + document.getElementById("inputElem").value; // Concatenates
1 + Number(document.getElementById("inputElem").value); // Adds
// Infinity if no args in Math.min or Math.max
Math.max(); // -Infinity
Math.min(); // Infinity
// If you use the new keyword to construct one of these types,
// what you actually get is an object of type Object that inherits the prototype
// of the type you want to construct (the exception is Function).
// So although you can construct a Number using the new keyword, it'll be of type Object;
typeof new Number(123); // "object"
typeof Number(123); // "number"
typeof 123; // "number"
// There are two object states that represent the lack of a value, null and undefined.
// This is pretty confusing for a programmer coming from another language like C#.
// You might expect the following to be true:
var a;
a === null; //false
a === undefined; //true
// Number + Number -> addition
1 + 2 // 3
// Boolean + Number -> addition
true + 1 // 2
// Boolean + Boolean -> addition
false + false // 0
// Number + String -> concatenation
5 + 'foo' // "5foo"
// String + Boolean -> concatenation
'foo' + false // "foofalse"
// String + String -> concatenation
'foo' + 'bar' // "foobar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment