<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Can you explain why this works in JavaScript?</title>
	<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript/</link>
	<description>Comments on Ask MetaFilter post Can you explain why this works in JavaScript?</description>
	<pubDate>Sat, 08 Sep 2012 10:19:52 -0800</pubDate>
	<lastBuildDate>Sat, 08 Sep 2012 10:29:40 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Can you explain why this works in JavaScript?</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript</link>	
		<description>Can you explain why this works in JavaScript? &lt;br /&gt;&lt;br /&gt; Someone posted this, saying the variable char will be assigned the value &quot;w&quot;&lt;br&gt;
&lt;br&gt;
var str=&quot;Hello, world!&quot;;&lt;br&gt;
var char=str[7];&lt;br&gt;
&lt;br&gt;
Since the variable str isn&apos;t explicitly defined as an array, and the characters of the string aren&apos;t explicitly defined as elements, I didn&apos;t believe it would work. But it does!&lt;br&gt;
&lt;br&gt;
Can you explain the syntax to me that makes this work? Is any string variable seen as an array by Javascript?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2012:site.224057</guid>
		<pubDate>Sat, 08 Sep 2012 10:19:52 -0800</pubDate>
		<dc:creator>markcmyers</dc:creator>
		
			<category>JavaScript</category>
		
			<category>resolved</category>
		
	</item>
	<item>
		<title>By: XMLicious</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240198</link>	
		<description>&lt;em&gt;&lt;q&gt;Is any string variable seen as an array by Javascript?&lt;/q&gt;&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
Yes.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240198</guid>
		<pubDate>Sat, 08 Sep 2012 10:29:40 -0800</pubDate>
		<dc:creator>XMLicious</dc:creator>
	</item><item>
		<title>By: pla</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240200</link>	
		<description>Because:&lt;br&gt;
0) You assigned str from a static string, so have effectively assigned the &lt;i&gt;type&lt;/i&gt;, as well, and...&lt;br&gt;
1) Javascript has insanely loose typing rules, and...&lt;br&gt;
2) Javascript inherits most of its syntax from C, where strings don&apos;t exist except as character arrays, and...&lt;br&gt;
3) Strings in JS have an implicit dereference overload (or just plain &quot;load&quot;, in this case?  What do you call a non-overloaded overload?) on using square brackets.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240200</guid>
		<pubDate>Sat, 08 Sep 2012 10:30:29 -0800</pubDate>
		<dc:creator>pla</dc:creator>
	</item><item>
		<title>By: TheOtherGuy</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240201</link>	
		<description>Javascript has &lt;a href=&quot;http://en.wikipedia.org/wiki/Duck_typing&quot;&gt;Duck Typing&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
The interpreter sees you want to treat the string like an array, and so invokes a string method to treat it as such rather than just throw an error .</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240201</guid>
		<pubDate>Sat, 08 Sep 2012 10:30:38 -0800</pubDate>
		<dc:creator>TheOtherGuy</dc:creator>
	</item><item>
		<title>By: weston</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240202</link>	
		<description>It actually doesn&apos;t work in every JS implementation. Older versions of IE, for example (where you&apos;d have to use the &lt;code&gt;.charAt&lt;/code&gt; method).&lt;br&gt;
&lt;br&gt;
Where it does work, though, it&apos;s not so much that Strings are seen as Arrays as that Strings just have an array-like facility for indexing.&lt;br&gt;
&lt;br&gt;
There are a couple of other &quot;types&quot; in JavaScript that are like this -- array-like things that are not arrays (function argument lists &amp;amp; DOM nodelists, for example) and therefore may not have *all* the facilities of arrays (particularly all the methods of Array), but have some array-like facilities, including the ability to be indexed with this syntax.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240202</guid>
		<pubDate>Sat, 08 Sep 2012 10:32:51 -0800</pubDate>
		<dc:creator>weston</dc:creator>
	</item><item>
		<title>By: markcmyers</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240204</link>	
		<description>Duck typing. Love it!&lt;br&gt;
&lt;br&gt;
Thanks, folks. I can sleep now.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240204</guid>
		<pubDate>Sat, 08 Sep 2012 10:35:05 -0800</pubDate>
		<dc:creator>markcmyers</dc:creator>
	</item><item>
		<title>By: jepler</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240209</link>	
		<description>ECMA-262 edition 5.1, the standard popularly known as &quot;javascript&quot; , specifies this behavior for String objects in 15.5.5.2, &quot;Properties of String Instances / [[GetOwnProperty]] ( P )&quot;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240209</guid>
		<pubDate>Sat, 08 Sep 2012 10:44:31 -0800</pubDate>
		<dc:creator>jepler</dc:creator>
	</item><item>
		<title>By: bitdamaged</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240222</link>	
		<description>I don&apos;t want to dive into this too much and this may get out of hand but I&apos;m going to argue that this isn&apos;t an issue with Javascript&apos;s loose type system or even Duck Typing.  This is a string being a string &lt;br&gt;
&lt;br&gt;
This instead is &lt;a href=&quot;http://en.wikipedia.org/wiki/Syntactic_sugar&quot;&gt;Syntactic Sugar&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
For most JS objects you can get its properties via dot notation or bracket notation.&lt;br&gt;
&lt;br&gt;
mystring = &quot;hello&quot;&lt;br&gt;
mystring[&apos;length&apos;] == 5&lt;br&gt;
mystring.length == 5&lt;br&gt;
&lt;br&gt;
Javascript&apos;s String Spec (from jepler) has this line&lt;br&gt;
&lt;br&gt;
&lt;em&gt;Let resultStr be a String of length 1, containing one character from str, specifically the character at position index, where the first (leftmost) character in str is considered to be at position 0, the next one at position 1, and so on.&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
which means if instead of doing mystring[&apos;&lt;em&gt;some property name&lt;/em&gt;&apos;] you do mystring[&apos;&lt;em&gt;Some Integer&lt;/em&gt;&apos;] it will return the character at that position.&lt;br&gt;
&lt;br&gt;
Its something that looks similar to what you&apos;re doing to an array which is why it exists but its just grammer its not loose or duck typing. Its more along the lines of the plus sign &quot;+&quot; which does addition when used with numbers but string concatenation when used with strings.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240222</guid>
		<pubDate>Sat, 08 Sep 2012 10:55:52 -0800</pubDate>
		<dc:creator>bitdamaged</dc:creator>
	</item><item>
		<title>By: nicwolff</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240324</link>	
		<description>Don&apos;t sleep yet! You&apos;ve Best-Answered contradictory answers here. XMLicious, pla, and TheOtherGuy are mistaken; the String object is never turned into or treated as an Array.&lt;br&gt;
&lt;br&gt;
weston, jepler, and bitdamaged have the correct answer: as described in &lt;a href=&quot;http://ecma-international.org/ecma-262/5.1/#sec-15.5.5&quot;&gt;section 15.5.5&lt;/a&gt; of the ECMAScript (aka JavaScript) specification, the String object overloads the default GetOwnProperty method that does object property lookups so that if the default method returns &lt;em&gt;undefined&lt;/em&gt; and the property is a number it returns the character at that position in the string.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240324</guid>
		<pubDate>Sat, 08 Sep 2012 13:13:19 -0800</pubDate>
		<dc:creator>nicwolff</dc:creator>
	</item><item>
		<title>By: markcmyers</title>
		<link>http://ask.metafilter.com/224057/Can-you-explain-why-this-works-in-JavaScript#3240350</link>	
		<description>I&apos;m always amazed by the erudition of the members of this site, which isn&apos;t even focused on coding. I&apos;m happy to have best-answered the early answers even if they aren&apos;t bullseyes, because they&apos;re close enough for someone like me, but I&apos;ve got to hand it to the rest of you, some of whom surely have enough knowledge to design your own languages.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2012:site.224057-3240350</guid>
		<pubDate>Sat, 08 Sep 2012 13:41:36 -0800</pubDate>
		<dc:creator>markcmyers</dc:creator>
	</item>
	</channel>
</rss>
