{"id":17474677,"url":"https://github.com/rohitdhatrak/javascriptquestions","last_synced_at":"2026-03-08T21:35:12.141Z","repository":{"id":43697227,"uuid":"378830790","full_name":"RohitDhatrak/JavascriptQuestions","owner":"RohitDhatrak","description":"A list of questions based on uncommon or tricky concepts in Javascript.","archived":false,"fork":false,"pushed_at":"2022-03-10T03:01:04.000Z","size":57,"stargazers_count":92,"open_issues_count":0,"forks_count":9,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-22T10:43:08.894Z","etag":null,"topics":["interview-preparation","javascript","javascript-questions"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RohitDhatrak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-21T06:35:53.000Z","updated_at":"2025-01-14T05:51:01.000Z","dependencies_parsed_at":"2022-08-02T20:15:08.770Z","dependency_job_id":null,"html_url":"https://github.com/RohitDhatrak/JavascriptQuestions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RohitDhatrak%2FJavascriptQuestions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RohitDhatrak%2FJavascriptQuestions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RohitDhatrak%2FJavascriptQuestions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RohitDhatrak%2FJavascriptQuestions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RohitDhatrak","download_url":"https://codeload.github.com/RohitDhatrak/JavascriptQuestions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250222336,"owners_count":21394853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interview-preparation","javascript","javascript-questions"],"created_at":"2024-10-18T18:23:44.088Z","updated_at":"2026-03-08T21:35:12.097Z","avatar_url":"https://github.com/RohitDhatrak.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"###### 1. What's the result?\n\n```javascript\nNumber(undefined)\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  NaN\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 2. What's the result?\n\n```javascript\nNumber(\"   123    \")\nNumber(\"12 3\")\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  123,\n  NaN (only whitespace from the start and end are removed)\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 3. What's the result?\n\n```javascript\nBoolean(\" \")\nBoolean(\"0\")\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  true,\n  true\n  \n  Only values that are intuitively “empty”, like 0, \"\", null, undefined, and NaN, become false.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 4. What's the result?\n\n```javascript\ntypeof null\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  object\n  \n  That’s an officially recognized error in typeof behavior, coming from the early days of JavaScript and kept for compatibility.\n  \n  Definitely, null is not an object. It is a special value with a separate type of its own.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 5. What's the result?\n\n```javascript\n2 ** 2\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  2² = 4\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 6. What's the result?\n\n```javascript\n2 + 2 + '1'\n'1' + 2 + 2\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  4 + '1' \u003e\u003e '41'\n  \n '12' + 2 \u003e\u003e '122'\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 7. What's the result?\n\n```javascript\n+true\n+\"\"\n+\"2\" + +\"3\"\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  1,\n  0,\n  5\n  \n  The plus + exists in two forms: the binary and the unary form.\n  \n  The unary plus doesn’t do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 8. What's the result?\n\n```javascript\nlet a = 1;\nlet b = 2;\n\nlet c = 3 - (a = b + 1);  \n// Good to understand how it works. Please don’t write the code like that. \n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  0\n  \n  All operators in JavaScript return a value. That’s obvious for + and -, but also true for =\n  \n  The call x = value writes the value into x and then returns it.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 9. What's the result?\n\n```javascript\nlet a = (1 + 2, 3 + 4);\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  a = 7\n  \n  The comma operator allows us to evaluate several expressions, dividing them with a comma ,. \n  Each of them is evaluated but only the result of the last one is returned.\n  \n  Why do we need an operator that throws away everything except the last expression?\n\n  Sometimes, people use it in more complex constructs to put several actions in one line.\n\n  For example:\n\n  ```javascript\n  // three operations in one line\n  for (a = 1, b = 3, c = a * b; a \u003c 10; a++) {\n   ...\n  }\n  // doesn't improve code readability so we should think well before using this.\n  ```\n\u003c/p\u003e\n\u003c/details\u003e\n    \n    \n###### 10. What's the result?\n\n```javascript\nlet y = \"5\";\nlet x = y++; // number or string 5?\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  number 5\n  \n  JavaScript first coerces the string to a number then assigns the value to the variable x and then increments the value.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 11. What's the result?\n\n```javascript\nlet a = NaN;\nlet b = NaN;\n\na === b;\na == b;\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  false, false\n  \n  NaN is the only value in JavaScript that is not equal to itself. So we can check for a NaN value by checking if the value is equal to itself.\n  According to IEEE standard NaN is not equal to NaN.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 12. What's the result?\n\n```javascript\nisNaN(\"this is a string not a NaN value\");\nNumber.isNaN(\"this is a string not a NaN value\");\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  true, false\n  \n  isNaN tries to coerce the value into a Number before checking if it's a NaN value. This issue has been fixed in Number.isNaN().\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 13. What's the result?\n\n```javascript\nlet negativeZero = -0;\n\nnegativeZero.toString();\nnegativeZero === 0;\nnegativeZero \u003c 0;\nnegativeZero \u003e 0;\n\nObject.is(negativeZero, -0)\nObject.is(negativeZero, 0)\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  \"0\", true, false, false\n  \n  When we try to use these operations we get some unexpected behaviour because language developers decided negative zero isn't needed.\n  \n  true, false\n  This was fixed in Object.is() method.\n  \n  Use case of -0: To show direction when something is stationary \n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 14. What's the result?\n\n```javascript\nlet string = 'orange';\n\nfunction changeToApple(string) {\n  string = 'apple';\n}\n\nchangeToApple(string);\n\nconsole.log(string);  // ??\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  orange\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 15. What's the result?\n\n```javascript\nconst promise = new Promise((resolve, reject) =\u003e {\n  console.log(1);\n  setTimeout(() =\u003e {\n    console.log(\"timerStart\");\n    resolve(\"success\");\n    console.log(\"timerEnd\");\n  }, 0);\n  console.log(2);\n});\npromise.then(console.log);\nconsole.log(3);\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1 2 3 timerStart timerEnd success\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 16. What's the result?\n\n```javascript\nlet fruit = prompt(\"Which fruit to buy?\", \"apple\");\n\nlet bag = {\n  [fruit]: 5,\n};\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  That’s called computed properties. The name of the property is taken from the variable fruit.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 17. What's the result?\n\n```javascript\nlet obj = {\n  for: 1,\n  let: 2,\n  return: 3\n};\n\nconsole.log( obj.for + obj.let + obj.return );\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  It will print 6. There are no limitations on property names. They can be any strings or symbols. Other types are automatically converted to strings. \n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 18. What's the result?\n\n```javascript\nlet obj = {\n  test: undefined\n};\n\nif(obj.test){\n  console.log(\"hi);\n}\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  It won't log hi because the property exists but the value is undefined. So we can use the `in` operator to check if a property is defined of not.\n \n  if(\"test\" in obj){ // returns true\n    console.log(\"hi\");\n  }\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 19. What's the result?\n\n```javascript\nlet codes = {\n  \"49\": \"Germany\",\n  \"41\": \"Switzerland\",\n  \"44\": \"Great Britain\",\n  // ..,\n  \"1\": \"USA\"\n};\n\nfor (let code in codes) {\n  console.log(code);\n}\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1, 41, 44, 49\n  integer properties are sorted (UTF-16 code units order), others appear in creation order.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 20. What's the result?\n\n```javascript\nlet a = {};\nlet b = a;\n\nlog( a == b );\nlog( a === b );\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  true, true\n  Both refer to the same object.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 21. What's does the User function return?\n\n```javascript\nfunction User(name) {\n  this.name = name;\n  this.isAdmin = false;\n}\n\nlet user = new User(\"Jack\");\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  The value of this is returned implicitly.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 22. What's the result?\n\n```javascript\nfunction User() {\n\n  if(new.target){\n    console.log(\"Hi\");\n  }\n}\n\nUser();\nnew User(); \n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1. undefined, 2. Hi\n  Inside a function, we can check whether it was called with new or without it, using a special new.target property.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 23. What's the result?\n\n```javascript\nfunction BigUser() {\n\n  this.name = \"John\";\n\n  return { name: \"Godzilla\" };\n}\n\nfunction SmallUser() {\n\n  this.name = \"John\";\n\n  return \"Rick\";\n}\n\nconsole.log( new BigUser().name );\nconsole.log( new SmallUser().name );\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  \n  \n  1. Godzilla, 2. Johm`\n  \n  If return is called with an object, then the object is returned instead of this\n  If return is called with a primitive, it’s ignored.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 24. Is the constructor call valid?\n\n```javascript\nfunction User(){\n  this.name = \"Admin\"\n}\nlet user = new User;\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  We can omit parentheses after new, if it has no arguments. Omitting parentheses here is not considered a “good style”, but the syntax is permitted by specification.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 25. Modify the code of up, down and showStep to make the calls chainable?\n\n```javascript\nlet ladder = {\n  step: 0,\n  up() {\n    this.step++;\n  },\n  down() {\n    this.step--;\n  },\n  showStep: function() {\n    alert( this.step );\n  }\n};\n\n// ladder.up().up().down().showStep(); // 1\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  We can return `this` in every method\n  ```javascript\n    let ladder = {\n    step: 0,\n    up() {\n      this.step++;\n      return this;\n    },\n    down() {\n      this.step--;\n      return this;\n    },\n    showStep() {\n      alert( this.step );\n      return this;\n    }\n  };```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 26. Is it possible to create functions A and B so that new A() == new B()??\n\n```javascript\nfunction A() { ... }\nfunction B() { ... }\n\nlet a = new A;\nlet b = new B;\n\nalert( a == b ); // true\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  Yes, it’s possible.\n\n  If a function returns an object then new returns it instead of this.\n\n  So they can, for instance, return the same externally defined object obj\n  ```javascript\n    let obj = {};\n\n    function A() { return obj; }\n    function B() { return obj; }\n\n    alert( new A() == new B() ); // true\n  ```\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 27. What's the result?\n\n```javascript\nlet user = {\n  address: null\n};\n\nlog( user?.name?.first );\nlog( user?.address?.street )\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1. undefined 2. undefined\n  \n  The optional chaining ?. stops the evaluation if the value before ?. is undefined or null and returns undefined\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 28. What's the result?\n\n```javascript\nlog(user?.address);\n\nlet userAdmin = {\n  admin() {\n    alert(\"I am admin\");\n  }\n};\n\nlet userGuest = {};\n\nlog(userAdmin.admin?.());\n\nlog(userGuest.admin?.());\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1. ReferenceError: user is not defined. If there’s no variable user at all, then user?.anything triggers an error\n  \n  2. I am admin 3. nothing happens\n  \n  The optional chaining ?. is not an operator, but a special syntax construct, that also works with functions and square brackets.\n  ?.(), ?.[]\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 29. Are the two statements valid?\n\n```javascript\ndelete user?.name;\nuser?.name = \"John\";\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1. valid 2. invalid\n  \n  We can use ?. for safe reading and deleting, but not writing\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 30. What's is valid in the below code?\n\n```javascript\n let numbers = {\n    0: 0\n }\n \n numbers.1 = 1;\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  the first part is valid the number 0 will automatically be converted to string. By specification, object property keys may be either of string type, or of symbol type.\n  \n  the `numbers.1 = 1;` is invalid\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 31. What's the result?\n\n```javascript\n let numbers = {\n    0: 0\n }\n \nlog(numbers.\"0\");\nlog(numbers[0]);\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1. error 2. returns 0\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 32. What's the result?\n\n```javascript\nalert( 1 || 0 );\nalert( null || 1 );\nalert( null || 0 || 1 );\nalert( undefined || null || 0 )\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  1, 1, 1, 0\n  \n  || returns the first truthy value (without any conversion) or the last one if no truthy value is found\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 33. What's the result?\n\n```javascript\nalert( 1 \u0026\u0026 0 );\nalert( 1 \u0026\u0026 5 );\nalert( null \u0026\u0026 5 );\nalert( 0 \u0026\u0026 \"no matter what\" );\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  0, 5, null, 0\n  \n  AND returns the first falsy value(without any conversion) or the last value if none were found\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 34. What's the result?\n\n```javascript\nalert( !!\"non-empty string\" );\nalert( !!null );\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n  true, false\n  \n  double NOT !! is sometimes used for converting a value to boolean type. The first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion.\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 35. What's the result?\n\n```javascript\nNaN ** 0\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n1\n\u003c/p\u003e\n\u003c/details\u003e\n\n###### 36. What's the result?\n\n```javascript\nlet n = 2;\nn *= 3 + 5\n```\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n16\n\u003c/p\u003e\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohitdhatrak%2Fjavascriptquestions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frohitdhatrak%2Fjavascriptquestions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohitdhatrak%2Fjavascriptquestions/lists"}