{"id":32769,"date":"2019-10-31T21:48:48","date_gmt":"2019-10-31T18:48:48","guid":{"rendered":"https:\/\/prohoster.info\/blog\/oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript\/"},"modified":"2019-10-31T21:48:48","modified_gmt":"2019-10-31T18:48:48","slug":"oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/news\/oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript","title":{"rendered":"Oops, I did it again: debugging common errors in JavaScript","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><img decoding=\"async\" alt=\"Oops, I did it again: debugging common errors in JavaScript\" src=\"\/wp-content\/uploads\/2019\/04\/1d3b45a6cf245d9f55f5a69c9b477d88.jpg\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n <br \/>\nSometimes writing JavaScript code can be challenging, and for many developers, it can even be intimidating. Throughout the development process, errors inevitably arise, and some of them tend to repeat frequently. This article is aimed at beginner developers and discusses these errors and their solutions. To illustrate, the names of functions, properties, and objects are taken from <noindex><a rel=\"nofollow\" href=\"https:\/\/genius.com\/Britney-spears-lucky-lyrics\">a popular song<\/a><\/noindex>. All of this helps to quickly remember how to fix common mistakes. <br \/>\n<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<blockquote><p><b>Reminder:<\/b> <i>for all readers of 'Habr' - a discount of 10,000 rubles when enrolling in any Skillbox course with the promo code 'Habr'.<\/i><\/p>\n<p><b>Skillbox recommends:<\/b> Practical Course <noindex><a rel=\"nofollow\" href=\"https:\/\/skillbox.ru\/agima\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=AGIMA&amp;utm_content=articles&amp;utm_term=oops\">\"Mobile Developer PRO\"<\/a><\/noindex>.\n<\/p><\/blockquote>\n<p><\/p>\n<h3>TypeError: property is undefined<\/h3>\n<p><\/p>\n<pre><code class=\"javascript\">let girl = {\n    name: \"Lucky\",\n    location: \"Hollywood\",\n    profession: \"star\",\n    thingsMissingInHerLife: true,\n    lovely: true,\n    cry: function() {\n        return \"cry, cry, cries in her lonely heart\"\n    }\n}\nconsole.log(girl.named.lucky)<\/code><\/pre>\n<p>\nThe above code sample throws the error Uncaught TypeError: Cannot read property \u2018lucky\u2019 of undefined. The problem is that the girl object does not have a named property, although it does have a name property. And since the girl.named property is undefined, it cannot be accessed, as it formally does not exist. However, if we replace girl.named.lucky with girl.name, everything will work and the program will return Lucky.<\/p>\n<p>Learn more about properties <noindex><a rel=\"nofollow\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Working_with_Objects\">read here<\/a><\/noindex>.<\/p>\n<h3>Ways to fix TypeError errors<\/h3>\n<p>\nTypeError errors occur when a programmer attempts to perform actions with data that do not match a specified type. For example, this can happen when using .bold(), accessing an undefined property, or calling a function that is actually not a function.<\/p>\n<p>So, if you try to call girl (), you will encounter the error Uncaught TypeError: yourVariable.bold is not a function and girl is not a function, since an object is actually being called, not a function.<\/p>\n<p>To resolve errors, you need to study the variables. So, what is girl? And what is girl.named? You can find out by analyzing the code, outputting the variables using console.log, the debugger command, or by calling the variable name in the console. You need to ensure that you can operate with the data type contained in the variable. If it doesn't fit, modify it, for example, by adding a condition or using a try..catch block \u2014 and you will gain control over the execution of the operation.<\/p>\n<h3>Stack overflow<\/h3>\n<p>\nIf we believe the authors of the song Baby One More Time (that's Britney Spears, yes), the word hit in this context means the singer's desire to be called again (this provides clarification about the song's context, \u2014 translator's note). It's quite possible that this desire will lead to an increased number of calls in real life. However, in programming, this refers to recursion, which can cause an error in case of a stack overflow.<\/p>\n<p>Errors appear as follows:<\/p>\n<p><i>Error: Out of stack space (Edge)<br \/>\nInternalError: too much recursion (Firefox)<br \/>\nRangeError: Maximum call stack size exceeded (Chrome)<br \/>\n <\/i><br \/>\nA stack overflow occurs if the developer did not account for the base case in recursion or if the code does not handle the intended case.<\/p>\n<pre><code class=\"javascript\">function oneMoreTime(stillBelieve=true, loneliness=0) {\n    if (!stillBelieve &amp;&amp; loneliness &lt; 0) return\n    loneliness++\n    return oneMoreTime(stillBelieve, loneliness)\n}<\/code><\/pre>\n<p>\nIn this case, stillBelieve can never be false, so oneMoreTime will be called every time, but the function will never complete.<\/p>\n<p>If you start hoping for two friends, it will reduce loneliness, and the call may not be needed.<\/p>\n<pre><code class=\"javascript\">function oneMoreTime(stillBelieve=true, loneliness=0) {\n    if (!stillBelieve &amp;&amp; loneliness &lt; 0) return\n    loneliness--\n    stillBelieve = false\n    return oneMoreTime(stillBelieve, loneliness)\n}<\/code><\/pre>\n<p>\nAn example can be made with infinite loops, where the system does not provide an error message, and the page running the JavaScript simply hangs. This happens if the while loop does not have a termination condition.<\/p>\n<pre><code class=\"javascript\">let worldEnded = false\n \nwhile (worldEnded !== true) {\n  console.log(\"Keep on dancin' till the world ends\")\n}<\/code><\/pre>\n<p>\nThe problem can be solved as follows:<\/p>\n<pre><code class=\"javascript\">let worldEnded = false\n \nwhile (worldEnded !== true) {\n  console.log(\"Keep on dancin' till the world ends\")\n  worldEnded = true\n}<\/code><\/pre>\n<p><\/p>\n<h3>Debugging Infinite Loops and Recursion<\/h3>\n<p>\nIf there is a problem with an infinite loop, in Chrome or Edge, you need to close the tab, and in Firefox \u2014 the browser window. After that, carefully analyze the code. If you cannot find the problem, it's advisable to add the debugger command in the loop or function and check the values of the variables. If the result does not match what is expected, replace it, which can be easily done.<\/p>\n<p>In the example mentioned above, the debugger should be added as the first line of a function or loop. Then, you need to open the debug tab in Chrome and analyze the variables in the scope. Using the next button allows you to track their changes during each iteration. It's all quite simple, and in most cases, the problem is found.<\/p>\n<p>You can read more about all this here (<noindex><a rel=\"nofollow\" href=\"https:\/\/developers.google.com\/web\/tools\/chrome-devtools\/javascript\/\">for Chrome<\/a><\/noindex>) and here (<noindex><a rel=\"nofollow\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Tools\/Debugger\">for Firefox<\/a><\/noindex>).<\/p>\n<h3>Syntax error<\/h3>\n<p>\nOne of the most common errors in JavaScript is SyntaxError. You can avoid them with text editor extensions. For example, Bracket Pair Colorizer highlights brackets in the code with different colors, while Prettier or a similar analysis tool allows you to quickly find errors. The best way to reduce the likelihood of encountering SyntaxError is minimal nesting.<\/p>\n<p>Share in the comments: what do you do to avoid making mistakes or to quickly detect and fix them?<\/p>\n<blockquote><p><b>Skillbox recommends:<\/b><\/p>\n<ul>\n<li>Two-Year Practical Course <noindex><a rel=\"nofollow\" href=\"https:\/\/iamwebdev.skillbox.ru\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=WEBDEVPRO&amp;utm_content=articles&amp;utm_term=oops\">\"I am a PRO Web Developer\"<\/a><\/noindex>.<\/li>\n<li>Online Course <noindex><a rel=\"nofollow\" href=\"https:\/\/skillbox.ru\/c-sharp\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=CSHDEV&amp;utm_content=articles&amp;utm_term=oops\">C# Developer from Scratch<\/a><\/noindex>.<\/li>\n<li>Practical Year Course <noindex><a rel=\"nofollow\" href=\"https:\/\/skillbox.ru\/php\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=PHPDEV&amp;utm_content=articles&amp;utm_term=oops\">\"PHP Developer from 0 to PRO\"<\/a><\/noindex>.\n<\/li>\n<\/ul>\n<\/blockquote>\n<p>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/skillbox\/blog\/450194\/\">habr.com<\/a><\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041f\u043e\u0440\u043e\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 JavaScript \u0434\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u043e, \u0430 \u0438\u043d\u043e\u0433\u0434\u0430 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0443\u0433\u0430\u0435\u0442, \u0447\u0442\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u043e \u043c\u043d\u043e\u0433\u0438\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c. \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043d\u0438\u0445 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u0435\u043d\u044c\u043a\u043e. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435, \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u043d\u043e\u0439 \u043d\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0431 \u044d\u0442\u0438\u0445 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u0445 \u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432\u0437\u044f\u0442\u044b \u0438\u0437 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u043f\u0435\u0441\u043d\u0438. \u0412\u0441\u0435 \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":24548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[702],"tags":[],"class_list":["post-32769","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041f\u043e\u0440\u043e\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/news\/oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47Oops, I did it again: \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 JavaScript | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041f\u043e\u0440\u043e\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/news\/oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-10-31T18:48:48+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-10-31T18:48:48+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Oops, I did it again: debugging common mistakes in JavaScript | ProHoster","description":"Sometimes, writing code.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/news\/oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47Oops, I did it again: \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 JavaScript | ProHoster","og:description":"\u041f\u043e\u0440\u043e\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430.","og:url":"https:\/\/prohoster.info\/en\/blog\/news\/oops-i-did-it-again-otladka-rasprostranennyh-oshibok-v-javascript","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2019-10-31T18:48:48+00:00","article:modified_time":"2019-10-31T18:48:48+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"32769","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":"2026-01-21 12:28:19","breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-03-01 02:52:26","updated":"2026-01-21 12:28:19","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/32769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=32769"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/32769\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media\/24548"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=32769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=32769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=32769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}