{"id":6591,"date":"2022-09-04T04:56:03","date_gmt":"2022-09-03T23:26:03","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/"},"modified":"2022-09-04T04:56:03","modified_gmt":"2022-09-03T23:26:03","slug":"solved-difference-between-typeof-andrei-and-typeof-andrei-valueof","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/","title":{"rendered":"[Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-44736515\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"44736515\" data-parentid=\"44736497\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>No, there&#8217;s no difference, unless someone&#8217;s done something silly to the JavaScript environment.<\/p>\n<p><code>\"andrei\"<\/code> is a string primitive, thus <code>typeof \"andrei\"<\/code> is <code>\"string\"<\/code>. If you do <code>\"andrei\".valueOf()<\/code>, you&#8217;re coercing a string primitive into a <code>String<\/code> object (in theory*) because you&#8217;re accessing a property on it (<code>valueOf<\/code>), and then asking that object for its primitive value\u00a0\u2014 which is a primitive string, thus <code>typeof \"andrei\".valueOf()<\/code> is <code>\"string\"<\/code>.<\/p>\n<p>Of course, you could replace <code>String.prototype.valueOf<\/code> with something other than the default which broke tne <code>String.prototype.valueOf<\/code> contract by returning, say, a number or object. But that&#8217;s out in bizarre-land.<\/p>\n<hr>\n<p>* &#8220;in theory&#8221; &#8211; JavaScript engines are free to optimize operations provided the optimization doesn&#8217;t make things behave in a way that isn&#8217;t according to spec. So a JavaScript engine could optimize away calling <code>.valueOf()<\/code> on a string primitive provided <code>String.prototype.valueOf<\/code> is still the same as its default value (which the engine can easily be aware of), since the result is just the same as the primitive. I don&#8217;t know whether they <em>do<\/em>, but they could.<\/p>\n<hr>\n<p>Re your comment on the question:<\/p>\n<blockquote>\n<pre><code> if((typeof name !=\"undefined\") &amp;&amp; (typeof name.valueOf()==\"string\") &amp;&amp; (name.length&gt;0))\n<\/code><\/pre>\n<p>in this one i <code>deleted valueOf()<\/code> and the result was the same and i was courious about that.<\/p>\n<\/blockquote>\n<p>Ah! That&#8217;s a different thing, for two reasons:<\/p>\n<ol>\n<li>\n<p><code>name<\/code> may not be a string. Lots of things have <code>length<\/code> properties\u00a0\u2014 strings, functions, basic arrays, typed arrays, DOM collections, any number of script-defined objects&#8230;<\/p>\n<\/li>\n<li>\n<p><code>name<\/code> may be a <code>String<\/code> <em>object<\/em> (although it&#8217;s unlikely, people don&#8217;t normally use <code>String<\/code> objects unless writing extension methods for <code>String.prototype<\/code>, in which case <code>this<\/code> refers to a <code>String<\/code> object).<\/p>\n<\/li>\n<\/ol>\n<p>So removing that <code>typeof name.valueOf()==\"string\"<\/code> part would change the condition. Re the <code>.valueOf()<\/code> part: The author of that code is either a bit paranoid or there&#8217;s a reason to expect that <code>name<\/code> may be a <code>String<\/code> object (and so its type would be <code>\"object\"<\/code>, not <code>\"string\"<\/code>). Using <code>valueOf<\/code> ensures that he\/she is checking the primitive instead.<\/p>\n<p>Surprisingly, they weren&#8217;t paranoid enough (at least, without context), since <code>null<\/code> would cause an exception in that code.<\/p>\n<p>The <code>()<\/code> around each condition are also completely unnecessary.<\/p>\n<p>Handling <code>null<\/code> as well looks like this:<\/p>\n<pre><code>if (name != null &amp;&amp; typeof name.valueOf() == \"string\" &amp;&amp; name.length &gt; 0)\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf() <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] No, there&#8217;s no difference, unless someone&#8217;s done something silly to the JavaScript environment. &#8220;andrei&#8221; is a string primitive, thus typeof &#8220;andrei&#8221; is &#8220;string&#8221;. If you do &#8220;andrei&#8221;.valueOf(), you&#8217;re coercing a string primitive into a String object (in theory*) because you&#8217;re accessing a property on it (valueOf), and then asking that object for its primitive &#8230; <a title=\"[Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\" aria-label=\"More on [Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333,362],"class_list":["post-6591","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] difference between typeof &quot;andrei&quot; and typeof &quot;andrei&quot;.valueOf() - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] difference between typeof &quot;andrei&quot; and typeof &quot;andrei&quot;.valueOf() - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] No, there&#8217;s no difference, unless someone&#8217;s done something silly to the JavaScript environment. &quot;andrei&quot; is a string primitive, thus typeof &quot;andrei&quot; is &quot;string&quot;. If you do &quot;andrei&quot;.valueOf(), you&#8217;re coercing a string primitive into a String object (in theory*) because you&#8217;re accessing a property on it (valueOf), and then asking that object for its primitive ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-03T23:26:03+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()\",\"datePublished\":\"2022-09-03T23:26:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\"},\"wordCount\":345,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"javascript\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\",\"name\":\"[Solved] difference between typeof \\\"andrei\\\" and typeof \\\"andrei\\\".valueOf() - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-03T23:26:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] difference between typeof \"andrei\" and typeof \"andrei\".valueOf() - JassWeb","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:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] difference between typeof \"andrei\" and typeof \"andrei\".valueOf() - JassWeb","og_description":"[ad_1] No, there&#8217;s no difference, unless someone&#8217;s done something silly to the JavaScript environment. \"andrei\" is a string primitive, thus typeof \"andrei\" is \"string\". If you do \"andrei\".valueOf(), you&#8217;re coercing a string primitive into a String object (in theory*) because you&#8217;re accessing a property on it (valueOf), and then asking that object for its primitive ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/","og_site_name":"JassWeb","article_published_time":"2022-09-03T23:26:03+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()","datePublished":"2022-09-03T23:26:03+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/"},"wordCount":345,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/","url":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/","name":"[Solved] difference between typeof \"andrei\" and typeof \"andrei\".valueOf() - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-03T23:26:03+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-difference-between-typeof-andrei-and-typeof-andrei-valueof\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] difference between typeof &#8220;andrei&#8221; and typeof &#8220;andrei&#8221;.valueOf()"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=6591"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6591\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=6591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=6591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=6591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}