{"id":23312,"date":"2020-09-28T08:26:10","date_gmt":"2020-09-28T15:26:10","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=23312"},"modified":"2021-01-04T06:16:53","modified_gmt":"2021-01-04T14:16:53","slug":"typeerror-cannot-read-property-length-of-undefined","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/","title":{"rendered":"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined"},"content":{"rendered":"\n<p>The <code>.length <\/code>property in JavaScript has many different use cases. It can be used to:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Indicate the number of parameters expected by a function with <code>function.length<\/code>.<\/li><li>Return the number of elements inside of an array with <code>Array.prototype.length<\/code>.<\/li><li>Find out the number of arguments passed into a function with <code>arguments.length.<\/code><\/li><li>Determine the number of characters inside of a string.<\/li><\/ol>\n\n\n\n<p>The length property returns a number for essentially any object in JavaScript.<br><\/p>\n\n\n\n<p>Typeerrors can be thrown when attempting to change a value that cannot be changed or when using a value in an inappropriate way. It can also occur when an argument is passed to a function that is incompatible with the type expected by the function or the operator inside of the function.<br><\/p>\n\n\n\n<p>In this article, we will dig into the cause of receiving a typeerror while using .length, as well as some possible solutions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why the Typeerror: Cannot Read Property \u2018length\u2019 of Undefined Gets Triggered<\/h2>\n\n\n\n<p>A typeerror length of undefined can indicate that the actual type of an <a href=\"https:\/\/careerkarma.com\/blog\/js-comparison\/\">operand<\/a> is different from the expected type. This error can be triggered with .length if the code is trying to access the property before it has been defined, so there needs to be a condition for when it returns undefined, and for when it does not return undefined. In most cases, unless dealing with an external <a href=\"https:\/\/careerkarma.com\/blog\/what-is-an-api\/\">API<\/a>,&nbsp; we can also just make sure to define it.<br><\/p>\n\n\n\n<p>The code below will throw a typeerror length of undefined because we are not passing an argument into the function in our console.log.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function readingErrorsAreImportant(a){\n if(a.length === 5){\n   return &quot;equals five&quot;\n } else if(a.length &gt; 5) {\n   return &quot;Greater than five&quot;\n }  else {\n   return &quot;Less than five&quot;\n }\n}\nconsole.log(readingErrorsAreImportant())\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>The simplest fix to this error is to make sure we are defining what we want to compare length to, so we need to make certain we are passing in an argument when we invoke the function inside of our console.log.<br><\/p>\n\n\n\n<p>Below we are passing in an array, but we can change this to just about any object .length can return a number for.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function readingErrorsAreImportant(a){\n if(a.length === 5){\n   return &quot;Equals five&quot;\n } else if(a.length &gt; 5) {\n   return &quot;Greater than five&quot;\n }  else {\n   return &quot;Less than five&quot;\n }\n}\nconsole.log(readingErrorsAreImportant([1, 2, 3, 4, 5]))\n<\/pre><\/div>\n\n\n\n<p>In a more advanced case where we are accessing an external API, we may want to indicate in our condition that if we get a length of undefined, then we can return something else to display to the user. However, we can also do this with a modified version of the example above.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function readingErrorsAreImportant(a){\n if(undefined !== a &amp;&amp; a.length){\n   return &quot;if undefined does not equal a and the length of a, I will be returned.&quot;\n } else {\n   return &quot;else, or if it is equal to undefined, I will be returned. This is to prevent a typeerror.&quot;\n }\n}\nconsole.log(readingErrorsAreImportant())\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion&nbsp;<\/h2>\n\n\n\n<p>Typeerrors generate when an operation cannot be performed. When they are triggered with the .length property, it is important to make sure that the object it is trying to return a number for is of the correct type and that it is defined. When trying to get the length of an object developers know may sometimes be undefined, including this condition in an if statement can prevent the error from being thrown.<br><\/p>\n","protected":false},"excerpt":{"rendered":"The .length property in JavaScript has many different use cases. It can be used to: Indicate the number of parameters expected by a function with function.length.Return the number of elements inside of an array with Array.prototype.length.Find out the number of arguments passed into a function with arguments.length.Determine the number of characters inside of a string.&hellip;","protected":false},"author":91,"featured_media":21615,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-23312","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Typeerror: Cannot Read Property \u2018length\u2019 of Undefined | Career Karma<\/title>\n<meta name=\"description\" content=\"On Career Karma, learn why you\u2019re getting that #JavaScript typeerror and how to fix it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined\" \/>\n<meta property=\"og:description\" content=\"On Career Karma, learn why you\u2019re getting that #JavaScript typeerror and how to fix it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-28T15:26:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-04T14:16:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kelly M.\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/misskellymore\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kelly M.\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\"},\"author\":{\"name\":\"Kelly M.\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/1cc6a89c78a56b632b6032b3b040c4fb\"},\"headline\":\"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined\",\"datePublished\":\"2020-09-28T15:26:10+00:00\",\"dateModified\":\"2021-01-04T14:16:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\"},\"wordCount\":457,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\",\"name\":\"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"datePublished\":\"2020-09-28T15:26:10+00:00\",\"dateModified\":\"2021-01-04T14:16:53+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/1cc6a89c78a56b632b6032b3b040c4fb\"},\"description\":\"On Career Karma, learn why you\u2019re getting that #JavaScript typeerror and how to fix it.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"width\":1020,\"height\":680,\"caption\":\"A black and silver laptop with code running on a table next to a plant and a yellow mug.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/careerkarma.com\/blog\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/1cc6a89c78a56b632b6032b3b040c4fb\",\"name\":\"Kelly M.\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/11\/kelly-moreira-150x150.jpeg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/11\/kelly-moreira-150x150.jpeg\",\"caption\":\"Kelly M.\"},\"description\":\"Kelly is a technical writer at Career Karma, where she writes tutorials on a variety of topics. She attended the University of Central Florida, earning a BS in Business Administration. Shortly after, she attended Lambda School, specializing in full stack web development and computer science. Before joining Career Karma in September 2020, Kelly worked as a Developer Advocate at Dwolla and as a team lead at Lambda School. Her technical writing can be found on Codecademy, gitConnected, and JavaScript in Plain English.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/kemore\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/misskellymore\"],\"url\":\"https:\/\/careerkarma.com\/blog\/author\/kelly-m\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined | Career Karma","description":"On Career Karma, learn why you\u2019re getting that #JavaScript typeerror and how to fix it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/","og_locale":"en_US","og_type":"article","og_title":"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined","og_description":"On Career Karma, learn why you\u2019re getting that #JavaScript typeerror and how to fix it.","og_url":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-09-28T15:26:10+00:00","article_modified_time":"2021-01-04T14:16:53+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","type":"image\/jpeg"}],"author":"Kelly M.","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/misskellymore","twitter_site":"@career_karma","twitter_misc":{"Written by":"Kelly M.","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/"},"author":{"name":"Kelly M.","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/1cc6a89c78a56b632b6032b3b040c4fb"},"headline":"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined","datePublished":"2020-09-28T15:26:10+00:00","dateModified":"2021-01-04T14:16:53+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/"},"wordCount":457,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/","url":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/","name":"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","datePublished":"2020-09-28T15:26:10+00:00","dateModified":"2021-01-04T14:16:53+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/1cc6a89c78a56b632b6032b3b040c4fb"},"description":"On Career Karma, learn why you\u2019re getting that #JavaScript typeerror and how to fix it.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","width":1020,"height":680,"caption":"A black and silver laptop with code running on a table next to a plant and a yellow mug."},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/typeerror-cannot-read-property-length-of-undefined\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"Typeerror: Cannot Read Property \u2018length\u2019 of Undefined"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/1cc6a89c78a56b632b6032b3b040c4fb","name":"Kelly M.","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/11\/kelly-moreira-150x150.jpeg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/11\/kelly-moreira-150x150.jpeg","caption":"Kelly M."},"description":"Kelly is a technical writer at Career Karma, where she writes tutorials on a variety of topics. She attended the University of Central Florida, earning a BS in Business Administration. Shortly after, she attended Lambda School, specializing in full stack web development and computer science. Before joining Career Karma in September 2020, Kelly worked as a Developer Advocate at Dwolla and as a team lead at Lambda School. Her technical writing can be found on Codecademy, gitConnected, and JavaScript in Plain English.","sameAs":["https:\/\/www.linkedin.com\/in\/kemore\/","https:\/\/x.com\/https:\/\/twitter.com\/misskellymore"],"url":"https:\/\/careerkarma.com\/blog\/author\/kelly-m\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/23312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/91"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=23312"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/23312\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/21615"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=23312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=23312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=23312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}