Basics Flashcards
What is coercision
Process of converting a variable to string for representational purpose
“99.99” == 99.99
True, the RHS is converted to string
What typing is used in JS
Dynamic typing
How to take only two values from a number
number.toFixed(2)
function outer() { var a = 1;
function inner() { var b = 2; console.log( a + b ); }
inner(); console.log( a ); }
outer();
Output of this problem
3
1
a = null;
typeof a;
what is the output
object
Declare array
var arr = [1,2,3];
what non boolean values when coerced to bool will give false value
0 -0 "" nan null undefined false
== checks for
Value equality, while allowing for coercion
=== checks
Value and type equality
obj a = { a:10}
obj b = {a:9}
a ==b
a===b
The checks are done only for reference equals and checking the underlying values
var a = [1,2,3]; var b = [1,2,3]; var c = "1,2,3";
a == c;
b == c;
a == b;
true
true
false
Arrays are coerced into string by default separated by commas
Scope of b and c and why?
function foo() { var a = 1;
if (a >= 1) { let b = 2;
while (b < 5) { let c = b * 2; b++;
console.log( a + c ); } } }
foo();
b will belong only to the if statement and thus not to the whole foo() function’s scope. Similarly, c belongs only to the while loop
Immediately Invoked Function Expressions (IIFEs) example
(function(){
return 10;
})();
Closure
Having one function inside other and the concept is the child function remebers the variable in scope for the parent function even after the parenr function has returned
What is the output
function foo() { console.log( this.bar ); }
var bar = “global”;
var obj1 = { bar: "obj1", foo: foo };
var obj2 = { bar: "obj2" };
// ——–
foo();
obj1.foo();
foo.call( obj2 );
new foo();
“global”
“obj1”
“obj2”
“Undefined” //because new creates a brand new this object
What does Object.Create() do ?
The Object.create() method creates a new object, using an existing object as the prototype of the newly created object
What is teh output ? x = 10 console.log(x !== x) x = NaN console.log(x !== x)
True
False
NaN the only value in the whole language that is not equal to itself.
What is Polyfilling?
Providing stubs for methods which are not present in the current version of JS
What is Transpiling?
Converting Newer JS syntax into a form the browser understands
How to get all objects passed to a function call ?
Every function has the “arguments” object, which can be indexed to get the values passed to it
Names of some Transpilers?
Babel and Traceur
document object,
console.log()
alert()what are they?
They are not provided by the JS engine, nor is it particularly controlled by the JavaScript specification.
They may be implemented in other languages and given to us by the browsers to use from JS code
how does anonymous function call itself to perform recursion
It can use “arguments.callee”, but it is deprcated