JS Puzzles
JS Puzzles
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('');
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);
5.
var bar = 1,
foo = {};
---------------------------
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.
// 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
-----------------------------------
// 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();
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);
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
-----------------------------------
console.log(UseMe);
var UseMe;
function UseMe()
{
console.log("UseMe function called");
}
-----------------------------------
(bar !== null) && (typeof bar === "object") && (bar.constructor.name !== 'Array'){
console.log('object');
}
-----------------------------------
tricky
typeof undefined == typeof NULL // true (because null is not case
sensitive)
typeof undefined === typeof NULL // true
-----------------------------------
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;
so when we define
a[b] = 11 {[object object]:11}
a[c] = 22 {[object object]:22}
so console.log(a[b]) // 22
-----------------------------------
SCOPE
-----------------
-----------------
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
})();
-------------------------
bar = 1 + 10 = 11
-------------------------
-------------------------
function sum(a, b) {
return a + b;
}
sum.foo = "Add";
-----------------------
var a = false;
for(i=0;i<5;i++){
var a = true;
}
if(a){
console.log(a);
}
// true
------------------------
ex-2
----------------------
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()
// 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()];
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);