You are on page 1of 10

1.

function bar() {
return foo;
foo = 10;
function foo() {}
var foo = '11';
}
alert(typeof bar())

Solution:----

function bar() {
function foo() {}
// function declaration are hoisted up
return foo;
foo = 10;
foo = '11';
}
alert(typeof bar())

// function

2.

[] + [] + 'foo'.split('');

"" + "" + ["f","o","o"];


"" + ["f","o","o"]
"f,o,o"

3.

new Array(5).toString();

",,,,"

4.

var x = 0;
function foo() {
x++;
this.x = x;
return foo;
}
var bar = new new foo;
console.log(bar.x);

// here foo is always returning itself.


so bar is always return function foo(){.....}
bar.x is undefined

5.

var bar = 1,
foo = {};

foo: { // this is not a object delaration


bar: 2;
baz: ++bar;
};
foo.baz + foo.bar + bar;

so undefined + undefined + 2 = NaN

---------------------------
var bar = 1;
foo = {
bar: 2,
baz: bar++
};

//
foo{bar:2, baz:1}

---------------------------
var bar = 1;
foo = {
bar: 2,
baz: ++bar
};

//
foo{bar:2, baz:2}

6.

var myArr = ['foo', 'bar', 'baz'];


myArr[2];
console.log("2" in myArr);

// in console.log(index in myArr)
true

7.

function foo(a, b) {
arguments[0] = 11;
arguments[1] = 22;
console.log(a,b)
}
foo(1,2);

// 11 22

function foo(a, b) {
//arguments[0] = 11;
//arguments[1] = 22;
console.log(a,b)
}
foo(1,2);

// 1 2

function foo(a, b) {
arguments[0] = 11;
arguments[1] = 22;
console.log(a,b)
}
foo();

// undefined undefined

-----------------------------------

VERY VERY IMP


Function declarations are hoisted over variable declarations but not over variable
assignments.

var double = 22;


function double(num) {
return (num*2);
}
console.log(double);

// 22

var double;
function double(num) {
return (num*2);
}
console.log(double);

// double(){...}

-----------------------------------
Very Imp

var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();

what happened here

var foo = 1;
function bar() {
var foo;
if (!foo) { // !undefined = !false = true;
var foo = 10;
}
alert(foo);
}
bar();

// here bar() is 10

-----------------------------------
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);

what happened here?

var a = 1;
function b() {
var a = function () {} // local
a = 10; // local
return;
}
b();
alert(a);

// 1

-----------------------------------
function foo(a) {
alert(arguments.length);
}
foo(1, 2, 3);

//3
-----------------------------------

Hoisting of functions before varables

Functions definition moves first before variables.

console.log(UseMe);
var UseMe;
function UseMe()
{
console.log("UseMe function called");
}

-----------------------------------

// How we achieve this.......typeof bar === "object"

(bar !== null) && (typeof bar === "object") && (bar.constructor.name !== 'Array'){
console.log('object');
}

-----------------------------------

undefined == null // true


undefined ==== null // false

typeof undefined == typeof null // false

tricky
typeof undefined == typeof NULL // true (because null is not case
sensitive)
typeof undefined === typeof NULL // true

-----------------------------------

Check number is interger?

function checkInt(n){
if(typeof n === "number"){
return true;
}
return false;
}

-----------------------------------

const a = {},
b = {c:'b'},
c = {b:'c'};

a[b] = 11;
a[c] = 22;

console.log(a[b])

Ans.

when we define

a[b] = 12;
a['m'] = 10;

a {[object object]:12, m:10}

so when we define
a[b] = 11 {[object object]:11}
a[c] = 22 {[object object]:22}

so console.log(a[b]) // 22

-----------------------------------

SCOPE

var foo = "bar";


function bar(){
var foo = "baz";
function baz(){
foo = "bam";
bam = "yay";
}
baz();
}
bar();
// foo = ?
// bam = ?
// baz() = ?
-----------------------------------

String("cool") === "cool"; // true


new String("cool") === "cool" // false

-----------------

const arr = [1, 2, 3];


const double = arr.map(function(el) {
return el * 2;
});
console.log(double); // [2, 4, 6]

-----------------

Drawback of creating methods in JavaScript?

A new copy of the method would be created for each instance.


ex.

function person(name){
this.name = name;
this.getName = function(){
return this.name;
}
}

So when we instance of person, the each instance have separate "getName" method

-------------------------

var x = 5;

(function () {
console.log(x);
var x = 10;
console.log(x);
})();

op:-
undefined , 10

this is like

(function () {
var x
console.log(x); // undefined
x = 10;
console.log(x); // 10
})();

-------------------------

var foo = 10;


bar = 3;
(function () {
var foo = 2;
bar = 1;
})()
bar = bar + foo;
console.log(bar);

bar = 1 + 10 = 11

-------------------------

var Foo = Function Bar()


{
return 7;
};
typeof Bar(); // Reference error

-------------------------

What will happen here?

function sum(a, b) {
return a + b;
}

sum.foo = "Add";

// foo property will added to function sum

-----------------------

var a = false;
for(i=0;i<5;i++){
var a = true;
}
if(a){
console.log(a);
}

// true

------------------------

Call by value / Call by Reference

call by value and call by reference


premitives are copied by their value
objects are copied by their reference

var number = 10;


function fun(number){
number++;
}
fun(number);
console.log(number); //10

ex-2

var obj = {value:10};


function fun(obj){
obj.value++;
}
fun(obj);
console.log(obj); // {value:11}

----------------------

var x = 1;
var y = x++;
console.log("x" + x, "y" + y)

// x2 y1

var x = 1;
var y = ++x;
console.log("x" + x, "y" + y)

// x2 y2

-------------------------------
Scopes

var a = 1;
function foo() {
var a = 10;
console.log(a);
}
console.log(a);
foo();

---------------
{
let foo = 10;
console.log( foo );
}
console.log( foo );

----------------
for (let i = 0; i < 5; i++) {
console.log( i );
}
console.log( i );

---------------
foo();
var foo;
function foo() {
console.log( 1 );
}
foo = function() {
console.log( 2 );
};
function foo() {
console.log( 3 );
}

----------------
var y = 1;
if (function f(){}) {
y += typeof f;
}
console.log(y);
----------------

Date()

// Javascript Add 110 minutes to current date and time

var d = new Date();


d.setMinutes(d.getMinutes()+100);

// Compare two timestamps

d1 = new Date("2014-01-15 00:00:00");


d2 = new Date("2014-01-12 00:00:00");
if(d1>d2){
console.log("d1 is greater")
}else if (d1<d2){
console.log("d2 is greater")
}else{
console.log("equal")
}

// millisecond to date
x = new Date();
var n = d.getTime();
var x = new Date(n);
alert(x);

----------------
Object tricky

var box = {}
box["material"] = "cool";
var func = function(){
return "material";
}
box[func()];

//pass object to a function

function person(name){
this.name = name;
}
var p1 = new person("sam");
function changeName(obj){
return obj.name = "new Name";
}
changeName(p1);

2.

function getSum(n1,n2){
return n1 + n2;
}
console.log(getSum.length) //2
---------------------
1.
var cat = function(x) {
return x * 2;
}
console.log(cat.name);

2.
blah = function () {
return lala;
};
console.log(blah());

3.
blah = function () {
return "lala";
};
console.log(blah());

4.
function blabbermouth() { };
console.log(blabbermouth.name);

5.
love_story = function(x, y) {
console.log(arguments);
};
love_story("princess", "frog");

6. Callback Functions
function c(f) {
return f();
}
function blub() {
return "monsters";
}
c(blub);

You might also like