Skip to content

kir0w/javascript-quiz

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 

Repository files navigation

JavaScript Quiz

The simple test for JavaScript developers:)

Table of Contents

  1. Labels & Blocks
  2. Semicolons
  3. Coercions
  4. Comparisons
  5. Inequalities
  6. Calculus
  7. Conditional statements
  8. Destructuring assignment
  9. void operator
  10. typeof operator
  11. , operator
  12. try-catch statement
  13. Hoisting
  14. Scopes & Closures & Hoisting
  15. Context
  16. delete operator
  17. ... operator
  18. instanceof operator
  19. Template literals
  20. Object
  21. Function
  22. Default parameters
  23. arguments
  24. class
  25. GeneratorFunction
  26. Promise
  27. AsyncFunction
  28. Reflect
  29. Proxy
  30. Array
  31. Date
  32. RegExp
  33. Symbol
  34. String
  35. Number
  36. Boolean
  37. Math
  38. Window
  39. Event loop
  40. eval method
  41. with operator
  42. Miscellaneous

Labels & Blocks

vars: var vars = vars;

vars;
foo: { foo = 123 };

foo;
var foo = {};
var bar = 1;

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

foo.baz + foo.bar + bar;
a: b: c: d: e: f: g: 1, 2, 3, 4, 5;
(printing => {
  printing: {
     console.log(1);
     if (!printing) {
       break printing;
     }
     console.log(2);
  };
  console.log(3);
})(false);

Back to top

Semicolons

var foo = [] // Missing semicolon!
(new Date).getTime();
!function(){console.log('awesome');}() // Missing semicolon!
!function(){console.log('language');}();
var [foo, bar, baz] = [1, 2, 3];

foo = bar // Missing semicolon!
++baz

[foo, bar, baz];
function foo() {
  return // Missing semicolon!
      'Some string';
}

foo() === 'Some string';
var bar = 'bar' // Missing semicolon!
/js/g.exec('awesome js!');

Back to top

Coercions

!!~1;
false % 1;
1 / '';
+'  -12';
+'1 2';
+'1'++;
'1' - - '1';
'5' + + '6';
'1' + 2 + '3' + 4;
'foo' + + 'bar';
4 - '5' + 0xf - '1e1';
!!!function() {};
4 + 3 + 2 + '1';
[1] + 1;
[] + 24 + 10;
'foo'.split('') + [];
[] + [] + 'foo'.split('');
[1, 2] + [3, 4];
[1, 2, [3, 4]] + [[5, 6], 7, 8];
[4] * [4];
({} + {});
[] + {};

[] * {};
{} + [];
[] + [];
++[];
[] + null + 1;
[4, 4] * [4, 4];
[3 + NaN + true] + [10 / (8 * null) - 1];
[][[]];
[[]][0];
++[[]][+[]];

Back to top

Comparisons

'3' - 2 === 1;
undefined == false;
[1] == true;
var x = true;
var y = false;

x != y === !(x == y);
!!'false' ==  !!'true';
!!'false' === !!'true';
1 + 2 + '3' == 1 + '2' + 3;
'001' == true;
[2] == true;
'00' == 0;
[true] == true;
0 === -0;
null == 0;
[1, 1] == true;
' \t\r\n' == 0;
'0x0' == false;
[[[[1]]]] == true;
'002' == true;
[] == ![];

Back to top

Inequalities

10 > 9 > 8;
9 < 5 < 1;
2 < 6 < 1;
1 < 3 < 4;
+0 < -0;
01 < 1;
[10] < [2];
'10' < '9';
'3' > '12'
'03' > '12';
Infinity > -Infinity;
({} + 'y' > {} + 'x');

Back to top

Calculus

0.3 - 0.1;
0.1 * 0.2;
.1 + .2 != .3;
0.1 + (0.2 + 0.3) == (0.1 + 0.2) + 0.3;
(0.2 + 0.4) / 1 == (0.1 + 0.2) * 2;
-9 % 7;
42 / -0;
var num1 = 5;
var num2 = 10;
var num3 = num1+++num2;

[num1, num2, num3];
var c = 0;

[c+++c++, c];
1 + - + + + - + 1;
01 - + - 02 - + - 03;
8 | 1;
~~(-5.5);
~~+'2.9'-1 == ('2.9' >> 0) - 1;
(1.22e-10).toFixed(2);
12..toFixed(1);
(0.9).toFixed(0);
(0.00008).toFixed(3) === 0;
parseInt('08');
parseInt(null);
parseInt(null, 24);
parseInt(Infinity);
parseInt(1 / 0, 19);
parseInt('Infinity', 23)
parseFloat('12.3.4');
parseFloat('\t\v\r12.34\n ');
parseFloat('-.00');

Back to top

Conditional statements

('false') && ('bar');
1 || 'foo' && 0;
1 && 3;
1 && 'foo' || 0;
var x, y;
x = (y = 0) && (y = 1);

[x, y];
var x = 1;
if (false)
    x = 3;
    x = 4;

x;
var foo = NaN;
switch (foo) {
  case NaN:
    console.log('NaN');
    break;
  default:
    console.log('default');
}
var foo = {};
switch (foo) {
  case {}:
    console.log('object');
    break;
  default:
    console.log('default');
}

Back to top

Destructuring assignment

var [x] = [];
x;
var [x = 1] = [];
x;
var [x, x, x, x] = 'JavaScript is awesome language'.split(' ');
x;
({ x, y } = { x: 5, y: 6, x: 7 });
var { x, y } = { x: x, y: y };

[x, y];
var x, { x: y = 1 } = { x };

[x, y];

Back to top

void operator

(() => void (1 + 1))();
void function() { return 'foo'; }();

Back to top

typeof operator

typeof 000;
typeof (null && false);
typeof (void null);
typeof { null: null }.null;
typeof setTimeout(() => {}, 0);
typeof typeof undefined;
var y = 1, x = y = typeof x;
x;
var x = [typeof x, typeof y][1];
typeof typeof x;

Back to top

Comma operator

(1, 2, 3);
var x = 0;
var y = (x++, 99);

[x, y];
(1, function(){})();
if (9, 0) console.log('ok');
[true, false][+true, +false];
(1,5 - 1) * 2;
var smth = (45, 87) > (195, 3) ? 'bar' : (54, 65) > (1, 0) ? '' : 'baz';
alert('1', alert('2', alert('3')));
typeof ('x', 3);
var f = (function f() { return '1' }, function g() { return 2 })();

typeof f;
[3, 4, 5, 6][1, 2];
if ((1, true) && (2, false) || (3, false) && (4, true) || (5, true)) {
  console.log('if');
} else {
  console.log('else');
}
alert('2',
  foo = (x) => alert(x),
  foo('1')
),
foo('3');

Back to top

try catch statement

(() => {
  try {
    return 'try';
  } finally {
    console.log('finally');
  }
})();
try {
  setTimeout(function() {
    throw new Error()
  }, 1000);
} catch (err) {}
try {
  try {
    throw new Error('try');
  } catch (err) {
    console.error('inner', err.message);
    throw err;
  } finally {
    console.log('finally');
  }
} catch (err) {
  console.error('outer', err.message);
}
(() => {
  label: try {
    return 0;
  } finally {
    break label;
    return 1;
  }
  return 2;
})();
(() => {
  try {
      throw new Error('try')
  } catch (err) {
      throw err; // The error thrown from try block is caught and rethrown
  } finally {
      throw new Error('finally'); // Finally(...) is thrown, which we did not expect
  }
})();

Back to top

Hoisting

var bar;
f();

function f() {
  return bar;
}

var bar = 2410;
(function() {
   console.log(x);
   console.log(f());

   var x = 1;
   function f() {
      return 2;
   }
})();
function f(x) {
  return x * 2;
}

var f;

typeof f;

Back to top

Scopes & Closures & Hoisting

var x = 1;
{
  var x = 2;
}

x;
if (!('y' in window)) {
  var y = 1;
}

y;
(function() {
  var x = x = 3;
})();

[typeof x, typeof y];
var foo = function bar() {
  return 'bar';
}

typeof bar();
var foo = 1;
function bar() {
  foo = 10;
  return;
  function foo() {};
}

bar();
foo;
x = 1;
(() => {
  return x;
  var x = 2;
})();
var x = 1;
if (function f() {}) {
  x += typeof f;
}

x;
(function f() {
  function f() { return 1; }
  return f();
  function f() { return 2; }
})();
for (var i = 0; i < 10; i++) {
  setTimeout(() => alert(i), 100);
}
var foo = 11;

function bar() {
  return foo;
  foo = 10;
  function foo() {};
  var foo = '12';
}

typeof bar();
var foo = 1,
var bar = function foo(x) {
  x && foo(--x);
};

foo;
(function() {
  var foo = 'x';

  (function (foo) {
    foo = 'y';
  })(foo);

  return foo;
})();
(function f(f) {
  return typeof f();
})(() => 1);
function f() {
  var x = 5;
  return new Function('y', 'return x + y');
}

f()(1);
var x = 1;

function f() {
  var x = 2;
  return new Function('', 'return x');
}

f()();

Back to top

Context

function f() {
  alert(this);
}

f.call(null);
function f() {
  return this;
}

f.call(f);
(function() {

  var f = function() {
    return this * 2;
  };

  return [
    f.call(undefined),
    f.call(null),
    f.call(1)
  ];

})();
var user = {
  name: 'Alex',
  _user: this
};

user._user.name;
var foo = {
  bar() { return this.baz },
  baz: 1
};

typeof (f = foo.bar)();
(function() {
  'use strict'

  var x = 10;
  var foo = {
    x: 20,
    bar() {
      var x = 30;
      return this.x;
    }
  };

  return [
    foo.bar(),
    (foo.bar)(),
    (foo.bar = foo.bar)(),
    (foo.bar, foo.bar)(),
    (foo.baz || foo.bar)()
  ];

})();
var foo = {
  bar: function() { return this === foo },
  baz: () => this === foo
};

foo.bar() === foo.baz();
var foo = {
  bar: 'bar',
  baz: !(function() {
    console.log(this.bar);
  })()
};

foo.baz;
(function() {
  'use strict';

  const g1 = (function() { return this || (1, eval)('this') })();
  const g2 = (function() { return this })();

  return g1 === g2;
}());

Back to top

delete operator

var numbers = [2, 3, 5, 7, 11, 13];
delete numbers[3];

numbers.length;
delete [].length;
function foo() {};
delete foo.length;

typeof foo.length;
var Person = function() {};
Person.prototype.type = 'person';

var admin = new Person();
delete admin.type;

admin.type;
delete delete window.document;
(function() {
  x = 1;
  window.y = 2;
  this.z = 3;
  var w = 4;

  delete x;
  delete y;
  delete z;
  delete w;

  return [typeof x, typeof y, typeof z, typeof w];
})();
(x => {
  delete x;
  return x;
})(1);
var x = 1;
y = 1;

(function() {
  return (delete window.x) === (delete window.y);
})();

Back to top

Spread operator

[...[...'...']].length
var foo = [...[,,24]];

[0 in foo, 1 in foo, 2 in foo];

Back to top

instanceof operator

function A() {};
function B() {};

A.prototype = B.prototype = {};

var a = new A();

a instanceof B;
function Rabbit() {};
var rabbit = new Rabbit();

Rabbit.prototype = {};

rabbit instanceof Rabbit;
function Animal() {};

function Rabbit() {};
Rabbit.prototype = Object.create(Animal.prototype);

var rabbit = new Rabbit();

rabbit instanceof Animal;
rabbit instanceof Object;
function f() { return f; }

new f() instanceof f;

Back to top

Template literals

`use strict`;

this == null;
typeof `${{Object}}`.prototype
((...args) => args)``
concat`this``is``a``test``!`();
var foo = { `hello world`: 24 };

Back to top

Object

Object instanceof Function;
Object.prototype.toString.call();
Object.prototype.toString.call(Object);
var proto = {
  foo: () => 'foo'
}

var obj1 = new Object(proto);
var obj2 = new Object(proto);

obj1 === obj2;
Object.is(NaN, NaN);
Object.is(-0, +0);
Object.assign({}, 'function');
Object.freeze(window);

var didItWork = true;

didItWork;
Object.prototype.isPrototypeOf(window);
({ 'toString': null }).propertyIsEnumerable('toString');
{ x: 1, y: 2 }['x'];
var obj = {
 '': ''
};

obj[''];
{ [{}]: {} };
var obj = {
  toString: () => '[object Foo]',
  valueOf: () => 2410
};

'object: ' + obj;
var obj = {
  1: 'foo'
};

obj[1] == obj[[1]];
obj[[1]] == obj['1'];
'foo'.someProperty = 17;

'foo'.someProperty;
(function() {
  var foo = {};
  var bar = {};
  var map = {};

  map[foo] = 'foo';
  map[bar] = 'bar';

  return map[foo];
})();
100['toString']['length'];
{ valueOf: () => true } == '1.0e0';
var x = 1;
var y = { toString: () => '1' };
var z = 1;

x + y+ z;

Back to top

Function

typeof Function;
Function instanceof Function;
Function instanceof Object;
typeof Function.prototype;
Function.prototype instanceof Function;
Function.prototype.isPrototypeOf(Function);
Function.prototype.prototype;
Function.prototype === Function;
Function.prototype.constructor === Function;
(function (x, y) {}).length;
(function (foo) {

  return typeof foo.bar;

})({ foo: { bar: 1 } });
var f = () => { x: 1 };
f();
function f() {};

f.bind(this).name;
var f = function() {};
var g = f.bind();

g.prototype;
function f(x, y) {
  return x + y;
}

f.bind(1)();
function f() {
  return this.value;
}

f.bind({ value: 1 }).call({ value: 2 });
var f = () => this;
var g = function() { return this }.bind(this);

f() === g();
(function() {
  if (false) {
    let f = { g() => 1 };
  }

  return typeof f;
})();
var foo = 2410;
var bar = ['a', 'b'];
var baz = { first: true };

function f(x, y, z) {
  x = 3;
  y.push('c');
  y = ['c'];
  z.first = false;
  z = true;
}

f(foo, bar, baz);

[foo, bar, baz.first];
var x = 0;

function f() {
  x++;
  this.x = x;
  return f;
}

var foo = new new f;

foo.x;
var f = Function.prototype.call;

f();
console.log.call.call.call.call.call.apply(x => x, [1, 2]);

Back to top

Default parameters

function f(x = 24, y = 10) {
  return x + y;
}

foo(undefined, 20);
foo(null, null);
function g(x, y = 24, z = 10) {
  return x + y + z;
}

g(1,,2);
var bar = 1;

function f(bar = bar) {
  return bar;
}

f();
var x = 1;

function f(y = function() { return x; }) {
  var x = 2;
  return y();
}

f();

Back to top

arguments

(function() {
  return typeof arguments;
})();
(function() {
  return arguments.toString();
})();
(function() {
  console.log(
    arguments.constructor === {}.constructor,
    arguments.constructor === [].constructor
  );
})();
(function() {
  var arguments;
  return arguments[0];
})(200);
function f(x, y, z) {
  arguments[2] = 10;
  return z;
}

f(1, 2, 3);
(() => arguments.toString())();
function f(x, y) {
  arguments[2] = 5;
  return y;
}

f(1);
function f(foo, bar, baz) {
  return Array.from(arguments);
}

f(...['foo', , 'bar']);
var g = function() {
  return arguments;
};

g() == g();

Back to top

Class

var Foo = class {};

class Foo {};
class AwesomeJS extends null {};

new AwesomeJS;
typeof (new (class F extends (String, Array) {})).substring;
typeof (new (class { class () {} }));
(function() {
  let f = this ? class g {} : class h {};
  return [
    typeof f,
    typeof h
  ];
})();

Back to top

Generator function

(function* f() { yield f })().next()

Back to top

Promise

typeof Promise;
typeof Promise.resolve(2410);
new Promise((resolve, reject) => resolve(24))
  .then((res) => console.log(res))
  .then((res) => console.log(res));
Promise.resolve('foo')
  .then(Promise.resolve('bar'))
  .then((res) => console.log(res));
Promise.resolve('foo')
  .then((res) => Promise.resolve('bar'))
  .then((res) => console.log(res));
Promise.resolve(2)
  .then(3)
  .then((res) => console.log(res));
Promise.resolve({ x: 24 })
  .then((res) => delete res.x)
  .then((res) => console.log(res.x));
Promise.resolve(2410).then(
  (res) => { throw new Error(res) },
  (err) => { try {} catch(err) {} }
);
Promise.reject(24)
  .then(null, null)
  .then(null, (reason) => console.log(reason));

Promise.reject(24)
  .then(10, null)
  .then(null, (reason) => console.log(reason));

Promise.reject(24)
  .then(null, 10)
  .then(null, (reason) => console.log(reason));
Promise.resolve(24)
  .then(null, null)
  .then((res) => console.log(res), null);

Promise.resolve(24)
  .then(null, 10)
  .then((res) => console.log(res), null);

Promise.resolve(24)
  .then(10, null)
  .then((res) => console.log(res), null);

Back to top

Async function

async function f(x) {
  return x * x;
}

f(2) === 4;
var y = 2;
async function f(x = await y) {
  return x * x;
}

f();
(() => {
  'use strict';
  async function eval(x, y) {
    await x * y;
  }

  eval(1, 2);
})();
async function f(x) {
  return await x * (await x);
}

f(2).then(res => console.log(res));
async function User(firstname, lastname) {
  this.firstname = await firstname;
  this.lastname = await lastname;
}

var user = new User('John', 'Doe');
(async function(){}).__proto__.__proto__ == (function(){}).__proto__;

Back to top

Reflect

Back to top

Proxy

Back to top

Array

typeof [1, 2, 3];
var arr = [];

arr instanceof Array;
arr instanceof Object;
var arr = [];

arr[1] = 1;
arr[24] = 24;

arr.length;
[].length = -2;
[,].length;
[,,].length;
[[[1], 2], 3].length;
new Array(null);
new Array(1)[0] + '';
new Array('100').length;
new Array(1, 2) == true;
new Array([], null, undefined, null) == ',,,';
[1, 2, 3].toString();
[1, 2, 3, 4, 5][0..toString().length];
[].unshift(0);
var arr = [2, 3, 5, 7, 11];

arr.indexOf(7, 2);
arr.indexOf(2, -3);
[NaN].indexOf(NaN);
'0'.split(void 0, 0);
Array(2).join();
[1, 2].join(undefined);
[,,,].join() === ',,';
Array(4).join('js!' - 4);
Array(5).join(',').length;
[].concat[1, 2, 3];
[].fill.call({ length: 3 }, 4);
Array.from({ '': '' });
[NaN].includes(NaN);
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

arr.slice();
arr.slice(-2);
arr.slice(3, -6);
Array(3).map(item => 'x');
Array.apply(null, new Array(4)).map((el, i) => i);
[...new Set([1, 1, 2, 3, 5, 8, 13])];
Array.prototype.push(1, 2, 3);
Array.isArray({
  constructor: Array,
  length: 0,
  __proto__: Array.prototype
});

Back to top

Date

'2016/12/31' == new Date('2016/12/31');
typeof (new Date() + new Date());
new Date(-666).getUTCMonth();
new Date(0) - 0;
new Date(0);
new Date('0');
new Date(0, 0);
new Date(0, 0, 0);

Back to top

RegExp

/[a-z]/ === /[a-z]/;
RegExp.prototype.toString = function() {
  return this.source;
}

/3/3/ - /1/;
'foobar'.replace(/^/, "$'");
/^$/.test('');
/$^/.test('');
/^.$/.test('');
/^..$/.test('');
'xy'.split(/x*?/);
'xy'.split(/(?:xy)*/);
'xy'.split(/x*/);
'.'.split(/(.?)(.?)/);
'.'.split(/()()/);
''.split(/.?/);
'test'.split(/(?:)/, -1);
typeof (/()??/).exec('')[1];
/^$^$^$^$^$^$$$$^^^/.test('');
new RegExp(/xy+z/, 'i');
var foo = /[/ + 'javascript'[0] + '///';
foo;

var bar = /\[/ + 'javascript'[0] + '///';
bar;

Back to top

Symbol

typeof Symbol;
var foo = new Symbol();
Symbol('foo') === Symbol('foo');
class MySymbol extends Symbol {
  constructor() {
    super();
  }
}

var symbol = new MySymbol();
Symbol.for('bar') === Symbol.for('bar');

Back to top

String

String(1.23e+2);
String(['foo', 'bar']);
'' instanceof String;
'foo' == new function() { return String('foo') };
'foo'[-1] == 'foo'.charAt(-1);
'foo'.charAt(1) === 'foo'.substring(1, 2);
++'24'.split('')[0];

Back to top

Number

3 instanceof Number;
Number.MIN_VALUE > 0;
Number();
Number(undefined);
Number([]);
Number([24, 10]);
Number({});
Number(['awesome', 'js']);
Number.isNaN('NaN') === window.isNaN('NaN');
Number.prototype.compare = function(value) {
  return this === value;
};

Number(123).compare(123);
Number.prototype.toString = function() {
  return typeof this;
};

(4).toString();

Back to top

Boolean

Boolean('true') === true;
Boolean([]);
Boolean('000');
Boolean(-Infinity);
if (new Boolean(false)) {
  console.log('JS');
}
new Boolean('').valueOf();
new Boolean(false).valueOf() === false;

Back to top

Math

Math instanceof Math;
Math.round(true + .501);
Math.round(-5.5);
Math.ceil(5.01) === -Math.floor(-5.01);
Math.max(2, []);
Math.max({}, 2);
Math.pow(+0, -1);
Math.pow(2, 53) === Math.pow(2, 53) + 1;
Math.pow(-0, -1);

Back to top

Window

typeof window;
typeof Window;
Object.getPrototypeOf(window) === Window;
Window.constructor === Function;
Window.prototype.constructor === Window;
(() => {
  console.log(window);
  var window = window;
})();

Back to top

Event loop

setTimeout(() => console.log(1), 1);
setTimeout(() => console.log(2), 1000);
setTimeout(() => console.log(3), 0);
console.log(4);
setTimeout(() => {
  console.log(1);
  setTimeout(() => console.log(2), 0);
}, 500);

setTimeout(() => {
  setTimeout(() => console.log(3), 500);
}, 250);
new Promise((resolve, reject) => {
  console.log(1);
  resolve();
}).then(() => {
  console.log(2);
});

console.log(3);
setTimeout(() => {
  console.log(1);
}, 300);

Promise.resolve()
  .then(() => console.log(2));

console.log(3);
setTimeout(() => {
  console.log(1);
}, 0);

Promise.resolve(2)
  .then((res) => {
    console.log(res);
    setTimeout(() => {
      console.log(3);
    }, 0);
    return Promise.resolve(4);
  })
  .then((res) => {
    console.log(res);
  });
setTimeout(() => {
  console.log(1);
}, 1000);

process.nextTick(() => {
  console.log(2);
});

console.log(3);
process.nextTick(() => {
  console.log(1);
  while (true) {}
});

process.nextTick(() => {
  console.log(2);
});

Back to top

eval method

function foo() {
  return eval.bind(null, '(function() { return bar; })');
}

var bar = 1;

(function() {
  var bar = 2;
  foo()()();
})();
function foo() {
  return '(function() { return bar; })';
}

var bar = 1;

(function() {
  var bar = 2;
  eval(foo())();
})();

Back to top

with operator

var a = 1;

var obj = {
  b: 2
};

with (obj) {
  var b;
  console.log(a + b);
}
with ({ a: 1 }) {
  a = 2,
  b = 3
}

[window.a, window.b];
with (function (x, undefined) {}) {
  length;
}
({
  x: 10,
  foo() {
    function bar() {
      console.log(x);      // => ?
      console.log(y);      // => ?
      console.log(this.x); // => ?
    }

    with (this) {
      var x = 20;
      var y = 30;
      bar.call(this);
    }
  }
}).foo();
var foo = { bar: 'baz' && 'foobarbaz' };
with (foo) var bar = eval('bar, 24');

foo.bar;

Back to top

Miscellaneous

(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]];
({[{}]:{[{}]:{}}})[{}][{}];
(function pewpew(Infinity, length, __proto__) {

  return [,,~0.[0|0]][pewpew.__proto__.length && Infinity,
                      -~String(this).length >> __proto__] << (0. === .0) + Infinity;

}).apply(typeof pewpew, [,,2]);

Back to top

About

❓ The simple test for JavaScript developers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published