Javascript 2 Flashcards
Why is the statement “use strict”; a string literal?
Because of backwards compatibility. There is no sideeffect to evaluating a string literal, so in engines that don’t support strict mode, they will work fine.
What is the biggest issue with ‘strict mode’;
When concatenating files for deployment, you may have libraries that don’t use strict mode, or files that don’t use strict mode. However, the act of concatenating them may cause files that weren’t written with strict mode in mind to have it enabled.
What is one way of getting around the “strict mode” file concatenation issue?
Wrap the code in immediately invoked function expressions and use “use strict” at the top of each function.
What is an IIFE?
Immediately Invoked Function Expression - a function that is called as soon as it is loaded - with no need for the user to call it explicitly.
What will the following produce:
var num1 = 1; var num2 = 2.34;
console. log(typeof num1);
console. log(typeof num2);
number
number
(NOTE: All javascript numbers are of type number)
All numbers in javascript are treated as 64bit (double precision floating point) numbers - with one exception. What is that?
When using bit wise operators. The number is converted to a 32bit, big-endian, twos compliment number
How would you print the value of a number in binary?
var num = 128; num.toString(2);
The number in the parameters defines the radix - 2 = binary
Are the following equivalent?
console. log((0.1 + 0.2) + 0.3); console. log(0.1 + (0.2 + 0.3));
No - they should be - but floating point math is inaccurate at best - and you can’t guarantee it is associative (unlike in maths, where it would be).
3 + true = 4 // This is true in Javascript
What is this process called?
Coercion, the values are coerced into the type appropriate to make the statement work.
Which operators perform coercian?
The maths operators - however, ‘+’ is a bit subtle, since when you use a string in the expression, it will convert the value to a string, rather than a number.
What does “2” + 3 == in Javascript?
“23”
Note - not a number
What does (1 + 2) + “3” == in javascript?
“33”
Because addition groups to the left
What is the biggest issue with coercions?
A number that is null won’t fail in a maths operation - it will be converted to 0. This could hide errors.
var x = NaN; x === NaN;
What is the result?
false
NaN is not equal to itself.
Because there are other values that can be coerced to NaN - what is the most reliable way for testing for NaN?
var a = NaN; a !== a; // true
This works because NaN is the only value in Javascript that is not equal to itself.
Write a utility function that tests for NaN - reliably.
function isReallyNaN(x) { return x !== x; }
Can an object be coerced into being a primitive?
Yes - particularly objects to strings
“the math object” + Math; // “the math object: [object Math]”
How is an object converted to a string?
By calling the toString method (this is done implicitly in a coercion, or the user can do it themselves as required).
What is the issue with the valueOf method in regards to coercion?
valueOf only really works well on objects that are supposed to be numbers (i.e. a Number object). So when adding two objects using the ‘+’ operator, javascript isn’t really sure of the intent, is it concatenation, or addition, and blindly calls the valueOf method - but this may not have been intended.
What is ‘truthiness’?
When an object is coerced into a boolean expression.
There are seven falsy values in Javascript - what are they?
Null, NaN, 0, -0, “”, false, and undefined
What is the error in the following code?
function point(x) { if (!x) { x = 100; } }
x is relying on coercion, unfortunately 0 is a valid number for this function, but would be evaluated to false. So we need to use a different test.
if (typeof x === undefined) {
x = 100;
}
Type errors can be hidden by…
implicit coercions
The + operator is overloaded to do addition or concatenation depending on…
it’s argument types