└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Essential JavaScript Interview Questions 2 | 3 | ### 1. Explain the result of output: 4 | 5 | ```javascript 6 | (function test () { 7 | console.log( 8 | {}.constructor === arguments.constructor, 9 | [].constructor === arguments.constructor 10 | ); 11 | })(); 12 | ``` 13 | 14 | ### 2. Explain the result of output: 15 | 16 | ```javascript 17 | (function test (arguments) { 18 | console.log(arguments[0]); 19 | })(100); 20 | ``` 21 | 22 | ```javascript 23 | (function test () { 24 | var arguments; 25 | console.log(arguments[0]); 26 | })(200); 27 | ``` 28 | 29 | ```javascript 30 | (function test() { 31 | function sum() { 32 | var sum = 0, i; 33 | for (i in arguments) { 34 | sum += i; 35 | } 36 | return sum; 37 | } 38 | 39 | console.log(sum(10, 20, 30, 40, 50)); 40 | })(); 41 | ``` 42 | 43 | ### 3. Explain the result of output: 44 | 45 | ```javascript 46 | (function test() { 47 | console.log( 48 | function () {} instanceof Object, 49 | function () {} instanceof Function, 50 | Function instanceof Object, 51 | Object instanceof Function 52 | ); 53 | })(); 54 | ``` 55 | 56 | ### 4. Explain the result of output: 57 | 58 | ```javascript 59 | (function test() { 60 | console.log( 61 | function () {}.apply.length 62 | ); 63 | })(); 64 | ``` 65 | 66 | ### 5. Explain the result of output: 67 | 68 | ```javascript 69 | (function test() { 70 | console.log( 71 | Function === Object.constructor, 72 | Function === Number.constructor, 73 | Function === Function.constructor, 74 | Function === Window.constructor, 75 | Function === Function.prototype.constructor, 76 | Object === Object.prototype.constructor, 77 | Number === Number.prototype.constructor, 78 | Array === Array.prototype.constructor, 79 | Window === Window.prototype.constructor 80 | ); 81 | })(); 82 | ``` 83 | 84 | ### 6. Explain the result of output: 85 | 86 | ```javascript 87 | (function test() { 88 | console.log( 89 | typeof Object.prototype, 90 | typeof String.prototype, 91 | typeof RegExp.prototype, 92 | typeof Function.prototype 93 | ); 94 | })(); 95 | ``` 96 | 97 | ```javascript 98 | (function test() { 99 | console.log( 100 | typeof undefined, 101 | typeof typeof undefined, 102 | typeof null, 103 | typeof 1 / null, 104 | typeof [], 105 | typeof {}, 106 | typeof document 107 | ); 108 | })(); 109 | ``` 110 | 111 | ```javascript 112 | (function test() { 113 | console.log( 114 | typeof Infinity, 115 | typeof NaN, 116 | typeof {null: null}.null, 117 | typeof {NaN: NaN}.NaN, 118 | typeof {Infinity: Infinity}.Infinity 119 | ); 120 | })(); 121 | ``` 122 | 123 | ### 7. Explain the result of output: 124 | 125 | ```javascript 126 | (function test() { 127 | var fn = function () { 128 | return this * 2; 129 | }; 130 | 131 | console.log(fn.apply(undefined)); 132 | console.log(fn.apply(null)); 133 | console.log(fn.apply(1)); 134 | })(); 135 | ``` 136 | 137 | ```javascript 138 | (function test() { 139 | 'use strict'; 140 | var fn = function () { 141 | return this * 2; 142 | }; 143 | 144 | console.log(fn.apply(undefined)); 145 | console.log(fn.apply(null)); 146 | console.log(fn.apply(1)); 147 | })(); 148 | ``` 149 | 150 | ### 8. Explain the result of output: 151 | 152 | ```javascript 153 | (function test() { 154 | console.log( 155 | Object.prototype.toString.call([]), 156 | Object.prototype.toString.call({}), 157 | Object.prototype.toString.call(Window), 158 | (16).toString(16), 159 | (true).toString(16), 160 | (false).toString(16) 161 | ); 162 | })(); 163 | ``` 164 | 165 | ### 9. Explain the result of output: 166 | 167 | ```javascript 168 | (function test() { 169 | var sum = function (a, b) { 170 | return a + b; 171 | }; 172 | console.log(typeof sum.call.apply); 173 | console.log(sum.call.apply(null, [1, 2])); 174 | })(); 175 | ``` 176 | 177 | ### 10. Explain the result of output: 178 | 179 | ```javascript 180 | (function test() { 181 | console.log(void (p = 1 / ""), p); 182 | })(); 183 | ``` 184 | 185 | ### 11. Explain the result of output: 186 | 187 | ```javascript 188 | (function test() { 189 | (function () { 190 | a = 1; 191 | var a = 2; 192 | })(); 193 | console.log(a); 194 | })(); 195 | ``` 196 | 197 | ```javascript 198 | (function test() { 199 | var a = 1; 200 | function test() { 201 | if (!a) { 202 | var a = 10; 203 | } 204 | console.log(a); 205 | } 206 | test(); 207 | console.log(a); 208 | })(); 209 | ``` 210 | 211 | ```javascript 212 | (function test() { 213 | (function () { 214 | var a = b = 3; 215 | })(); 216 | 217 | console.log( 218 | typeof a, 219 | typeof b 220 | ); 221 | })(); 222 | ``` 223 | 224 | ### 12. Which a variant is preferable and why? 225 | 226 | ```javascript 227 | (function test() { 228 | console.log(error !== undefined && error.x); 229 | })(); 230 | ``` 231 | 232 | ```javascript 233 | (function test() { 234 | console.log(typeof error !== 'undefined' && error.x); 235 | })(); 236 | ``` 237 | 238 | ### 13. Explain the result of output: 239 | 240 | ```javascript 241 | (function test() { 242 | var a = []; 243 | 244 | console.log( 245 | a.length, 246 | [,].length, 247 | [, ,].length 248 | ); 249 | 250 | a.length = -1; 251 | console.log(a.length); 252 | })(); 253 | ``` 254 | 255 | ```javascript 256 | (function test() { 257 | var a = [1, 2, 3, 4, 5]; 258 | a.length = null; 259 | 260 | console.log(a[4]); 261 | })(); 262 | ``` 263 | 264 | ```javascript 265 | (function test() { 266 | var a = [1, 2, 3, 4, 5]; 267 | a.length = undefined; 268 | 269 | console.log(a[4]); 270 | })(); 271 | ``` 272 | 273 | ```javascript 274 | (function test() { 275 | var a = [[[1], 2], 3]; 276 | 277 | console.log(a.length); 278 | })(); 279 | ``` 280 | 281 | ```javascript 282 | (function test() { 283 | console.log( 284 | [1,2,[3,4]] + [[5,6], 7, 8] 285 | ); 286 | })(); 287 | ``` 288 | 289 | ### 14. Explain the result of output: 290 | 291 | ```javascript 292 | (function test() { 293 | console.log( 294 | 9 < 5 < 1, 295 | 2 < 6 < 1, 296 | 1 < 3 < 4 297 | ); 298 | })(); 299 | ``` 300 | 301 | ### 15. Explain the result of output: 302 | 303 | ```javascript 304 | (function test() { 305 | function fn() { 306 | return 307 | { 308 | value: "test" 309 | } 310 | } 311 | 312 | console.log( 313 | typeof fn() 314 | ); 315 | })(); 316 | ``` 317 | 318 | ### 16. Explain the result of output: 319 | 320 | ```javascript 321 | (function test() { 322 | function sum(a, b) { 323 | return a + b; 324 | } 325 | 326 | function sum(c) { 327 | return c; 328 | } 329 | 330 | console.log(sum(3)); 331 | console.log(sum(2, 4)); 332 | })(); 333 | ``` 334 | 335 | ### 17. Explain the result of output: 336 | 337 | ```javascript 338 | (function test() { 339 | a = 1; 340 | window.b = 2; 341 | this.c = 3; 342 | var d = 4; 343 | 344 | delete a; 345 | delete b; 346 | delete c; 347 | delete d; 348 | 349 | console.log(typeof a, typeof b, typeof c, typeof d); 350 | })(); 351 | ``` 352 | 353 | ### 18. Explain the result of output: 354 | 355 | ```javascript 356 | (function test() { 357 | var a = 1; 358 | 359 | setTimeout(function () { 360 | a = 0; 361 | console.log('Hi!'); 362 | }, 0); 363 | 364 | while (a) { 365 | } 366 | console.log('Hello!'); 367 | })(); 368 | ``` 369 | 370 | ### 19. Explain the result of output: 371 | 372 | ```javascript 373 | (function test() { 374 | console.log( 375 | [] - [], 376 | [] + [], 377 | {} - {}, 378 | {} + {} 379 | ); 380 | })(); 381 | ``` 382 | 383 | ### 20. Explain the result of output: 384 | 385 | ```javascript 386 | (function test() { 387 | var holder = {value: 1}, 388 | holder2 = holder; 389 | 390 | holder.result = holder = {value: 0}; 391 | 392 | console.log( 393 | holder.result, 394 | holder2 395 | ); 396 | })(); 397 | ``` 398 | 399 | ### 21. Explain the result of output: 400 | 401 | ```javascript 402 | (function test() { 403 | var test = { 404 | property: 'Value', 405 | 406 | getPropertyValue: function () { 407 | return this.property; 408 | } 409 | }; 410 | 411 | var getPropertyValue = test.getPropertyValue; 412 | 413 | console.log( 414 | getPropertyValue(), 415 | test.getPropertyValue() 416 | ); 417 | })(); 418 | ``` 419 | 420 | ```javascript 421 | (function test () { 422 | "use strict"; 423 | 424 | var holder, fn; 425 | holder = { 426 | holderFn: function () { 427 | console.log(this); 428 | } 429 | }; 430 | fn = holder.holderFn; 431 | 432 | holder.holderFn(); 433 | fn(); 434 | })(); 435 | ``` 436 | 437 | ### 22. What is the maximum depth of the stack, starting with the "test" function? 438 | 439 | ```javascript 440 | 450 | ``` 451 | 452 | ```javascript 453 | var a = [1, 2, 3, 4, 5]; 454 | 455 | (function test() { 456 | console.log((new Error()).stack); 457 | 458 | var item = a.pop(); 459 | item && arguments.callee(); 460 | })(); 461 | ``` 462 | 463 | ### 23. Explain the result of output: 464 | 465 | ```javascript 466 | (function test() { 467 | function fn() { 468 | arguments.callee.count = arguments.callee.count || 0; 469 | return arguments.callee.count++; 470 | } 471 | 472 | console.log( 473 | fn(), 474 | fn(), 475 | fn() 476 | ); 477 | })(); 478 | ``` 479 | 480 | ### 24. Explain the result of output: 481 | 482 | ```javascript 483 | (function test() { 484 | var a = '5', b = 2, c = a+++b; 485 | console.log(c); 486 | })(); 487 | ``` 488 | 489 | ### 25. Explain the result of output: 490 | 491 | ```javascript 492 | console.log( 493 | 016 * 2, 494 | 0x16 * 2 495 | ); 496 | ``` 497 | 498 | ```javascript 499 | console.log( 500 | Math.floor(999.99) === ~~999.99, 501 | Math.floor(-999.99) === ~~-999.99, 502 | ~function(){}(), 503 | ~~function(){}(), 504 | ~~null, 505 | ~~undefined, 506 | ~~[], 507 | ~~{}, 508 | ~~'Test' 509 | ); 510 | ``` 511 | 512 | ```javascript 513 | console.log( 514 | 1 + "2" + "2", 515 | 1 + +"2" + "2", 516 | 1 + -"1" + "2", 517 | +"1" + "1" + "2", 518 | "2" * 3, 519 | "6" / 2, 520 | "A" - "B" + "2", 521 | "A" - "B" + 2 522 | ); 523 | ``` 524 | 525 | ```javascript 526 | console.log(0.1 + 0.2); 527 | console.log(0.1 + 0.2 === 0.3); 528 | console.log( 529 | (0.1 + 0.2) + 0.3 === 0.1 + (0.2 + 0.3) 530 | ); 531 | ``` 532 | 533 | ```javascript 534 | console.log( 535 | Number('Test!'), 536 | Number(''), 537 | Number('00010'), 538 | Number(true), 539 | Number(NaN), 540 | parseInt('2', 2), 541 | parseInt('011', 8), 542 | parseInt('011', 10), 543 | parseInt('00C', 16), 544 | parseInt([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]), 545 | 1 / -0, 546 | isNaN(1 / -0), 547 | isFinite(1 / -0), 548 | 0 / -0, 549 | isNaN(0 / -0), 550 | isFinite(0 / -0), 551 | 1 / 0, 552 | isNaN(1 / 0), 553 | isFinite(1 / 0), 554 | 0 / 0, 555 | isNaN(0 / 0), 556 | isFinite(0 / 0), 557 | NaN === NaN, 558 | Infinity === Infinity 559 | ); 560 | ``` 561 | 562 | ### 26. Explain the result of output: 563 | 564 | ```javascript 565 | console.log(true == [1] && true == [2]); 566 | ``` 567 | 568 | ```javascript 569 | console.log( 570 | new Boolean() == true, 571 | new Boolean("") == true, 572 | new Boolean("0") == true, 573 | new Boolean("1") == true, 574 | new Boolean("true") == true 575 | ); 576 | ``` 577 | 578 | ```javascript 579 | console.log( 580 | false == '0', 581 | false === '0', 582 | true == '1', 583 | true === '1', 584 | true == '2', 585 | true === '2' 586 | ); 587 | ``` 588 | 589 | ### 27. Explain the result of output: 590 | 591 | ```javascript 592 | (function () { 593 | var fn = function () { 594 | console.log(typeof this); 595 | }; 596 | fn.call("Hello World!"); 597 | })(); 598 | 599 | (function () { 600 | "use strict"; 601 | var fn = function () { 602 | console.log(typeof this); 603 | }; 604 | fn.call("Hello World!"); 605 | })(); 606 | ``` 607 | 608 | ```javascript 609 | console.log( 610 | (function () { 611 | return (new this).stack; 612 | }).apply(Error) 613 | ); 614 | ``` 615 | 616 | ### 28. Explain the result of output: 617 | 618 | ```javascript 619 | (function test() { 620 | var a = {}, 621 | b = {value: 'test1'}, 622 | c = {value: 'test2'}; 623 | 624 | a[b] = 'test3'; 625 | a[c] = 'test4'; 626 | 627 | console.log(a[b]); 628 | })(); 629 | ``` 630 | 631 | ### 29. Explain the result of output: 632 | 633 | ```javascript 634 | console.log( 635 | Date(), 636 | new Date, 637 | +Date(), 638 | +new Date, 639 | +new Date(), 640 | +new Date() === +new Date 641 | ); 642 | ``` 643 | 644 | ### 30. Explain the result of output: 645 | 646 | ```javascript 647 | console.log(typeof confirm('Do you like JavaScript?')); 648 | ``` 649 | 650 | ### 31. Eliminate non-existent state of promise. 651 | 652 | 1. fulfilled 653 | 2. awaiting 654 | 3. pending 655 | 4. refused 656 | 5. rejected 657 | 6. interrupted 658 | 659 | ### 32. Explain the result of output: 660 | 661 | ```javascript 662 | (function test() { 663 | console.log( 664 | [1, 2, 3, 4, 5].map(function (n) { 665 | return n === 1 && 1 || arguments.callee(n - 1) * n; 666 | }) 667 | ); 668 | })(); 669 | ``` 670 | 671 | ```javascript 672 | (function test() { 673 | "use strict"; 674 | console.log( 675 | [1, 2, 3, 4, 5].map(function (n) { 676 | return n === 1 && 1 || arguments.callee(n - 1) * n; 677 | }) 678 | ); 679 | })(); 680 | ``` 681 | 682 | ### 33. Explain the result of output: 683 | 684 | ```javascript 685 | (function test() { 686 | var s1 = 'test', 687 | s2 = new String('test'), 688 | s3 = String('test'); 689 | 690 | console.log( 691 | s1 == s2, 692 | s1 === s2, 693 | s1 == s3, 694 | s1 === s3, 695 | s1.constructor === s2.constructor, 696 | s1.constructor === s3.constructor, 697 | typeof s1, 698 | typeof s2, 699 | typeof s3 700 | ); 701 | 702 | console.log( 703 | s1.slice() == s1, 704 | s1.slice() == s2, 705 | s1.slice() == s3, 706 | s1.slice() === s1, 707 | s1.slice() === s2, 708 | s1.slice() === s3 709 | ); 710 | 711 | s1[2] = 'w'; 712 | console.log(s1); 713 | })(); 714 | ``` 715 | 716 | ### 34. Explain the result of output: 717 | 718 | ```javascript 719 | (function test() { 720 | var a = [1, 2, 3], 721 | b = a.reverse(), 722 | c = [4, 5, 6]; 723 | 724 | b.push(c); 725 | 726 | console.log(a.length, b.length); 727 | console.log(a.slice(-1)); 728 | console.log(b.slice(-1)); 729 | })(); 730 | ``` 731 | 732 | ### 35. Explain the result of output: 733 | 734 | ```javascript 735 | (function test() { 736 | for (var i = 0; i < 5; i++) { 737 | setTimeout(function () { 738 | console.log(i); 739 | }, 0) 740 | } 741 | })(); 742 | ``` 743 | 744 | ### 36. Explain the result of output: 745 | 746 | ```javascript 747 | 757 | ``` 758 | 759 | ```javascript 760 | 768 | ``` 769 | 770 | ```javascript 771 | 778 | ``` 779 | 780 | ### 37. Explain the result of output: 781 | 782 | ```javascript 783 | (function test() { 784 | var Factory = function () { 785 | var a = []; 786 | a[Array.prototype.pop.apply(arguments)] = 1; 787 | return a; 788 | }; 789 | 790 | console.log( 791 | Factory(0).length, 792 | Factory(100).length, 793 | Factory(Infinity).length, 794 | Factory(NaN).length 795 | ); 796 | })(); 797 | ``` 798 | 799 | ### 38. Explain the result of output when the page is fully loaded: 800 | 801 | ```html 802 | 803 |
804 | 811 | 812 | 813 | 814 | 815 | ``` 816 | 817 | ### 39. Do you see the pitfalls in the code? 818 | 819 | ```html 820 | 821 | 822 | 850 | 851 | 852 | ``` 853 | 854 | ### 40. How can we prevent a memory leak? 855 | 856 | ```javascript 857 | var Key = function (key) { 858 | this.key = key; 859 | }; 860 | 861 | var map = new Map(); 862 | 863 | function addToMap() { 864 | var currentKey = new Key(100); 865 | map.set(currentKey, {}); 866 | currentKey = null; 867 | 868 | setTimeout(addToMap); 869 | } 870 | 871 | setTimeout(addToMap); 872 | ``` --------------------------------------------------------------------------------