Skip to content

Commit 2c5f6bc

Browse files
committed
Merge pull request khan4019#59 from lucaslin/master
making minor sentence tweaks
2 parents 1e9ecd6 + 78b4cfb commit 2c5f6bc

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

js2.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,17 @@ <h4>null</h4>
157157
<div id="doubleVsTripleEqual">
158158
<h2>2. == Vs ===</h2>
159159
<p><strong>Question:</strong> What are the differences between <code>==</code> and <code>===</code>?</p>
160-
<p><strong>Answer: </strong> The simplest way of saying that, == will not check types and === will check whether both sides are of same type. So, == is tolerant. But under the hood it converts to its convenient type to have both in same type and then do the comparison.</p>
161-
<p>=== compares the types and values. Hence, if both sides are not same type, answer is always false. For example, if you are comparing two strings, they must have identical character sets. For other primitives (number, boolean) must share the same value.</p>
162-
<p><strong>Rule for implicit coercion:</strong> Comparison by using == does implicit type conversion under the hood. And rules for implicit coercion are as follows-</p>
160+
<p><strong>Answer: </strong> The simplest way of saying it is that == will not check types and === will check whether both sides are of the same type. So, == is tolerant. But under the hood it converts to a convenient type, in order to have both as the same type, and then does the comparison.</p>
161+
<p>=== compares the types and values. Hence, if both sides are not same type, the answer is always false. For example, if you are comparing two strings, they must have identical character sets. For other primitives (number, boolean), they must share the same value.</p>
162+
<p><strong>Rule for implicit coercion:</strong> Comparison by using == does implicit type conversion under the hood. The rules for implicit coercion are as follows-</p>
163163
<ul>
164164
<li>If both operands are same type use ===</li>
165165
<li>undefined == null</li>
166166
<li>If one operands is string another is number, convert string to number</li>
167167
<li>If one is boolean and another is non-boolean, convert boolean to number and then perform comparison</li>
168168
<li>While comparing a string or number to an object, try to convert the object to a primitive type and then try to compare</li>
169169
</ul>
170-
<p>Be careful while comparing objects, identifiers must reference the same objects or same array.</p>
170+
<p>Be careful while comparing objects identifiers must reference the same objects or same array.</p>
171171
<pre><code>
172172
var a = {a: 1};
173173
var b = {a: 1};
@@ -185,7 +185,7 @@ <h2>2. == Vs ===</h2>
185185
<h2>3. Object Equality</h2>
186186
<p><strong>Question: </strong> How would you compare two objects in JavaScript?</p>
187187
<p><strong>Basics:</strong> JavaScript has two different approaches for testing equality. Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and user defined objects are compared by their reference. This means it compares whether two objects are referring to the same location in memory.</p>
188-
<p><strong>Answer:</strong> Equality check will check whether two objects have same value for same property. To check that, you can get the keys for both the objects. If the number of properties doesn't match, these two objects are not equal. Secondly, you will check each property whether they have the same value. If all the properties have same value, they are equal.</p>
188+
<p><strong>Answer:</strong> Equality check will check whether two objects have the same value for the same property. To check that, you can get the keys for both the objects. If the number of properties doesn't match, these two objects are not equal. Secondly, you will check whether each property has the same value in both objects. If all the properties have the same value, they are equal.</p>
189189
<pre><code>
190190
function isEqual(a, b) {
191191
var aProps = Object.getOwnPropertyNames(a),
@@ -238,7 +238,7 @@ <h3>True False Rapid Fire</h3>
238238
<p><strong>Question: </strong> <code>Boolean(/foo/)</code></p>
239239
<p><strong>Answer: </strong> <code>true</code></p>
240240
<P><strong>Question:</strong> <code>true%1</code></P>
241-
<P><strong>Answer:</strong> 0. When you are trying to find reminder of true, true becomes 1 and reminder of 1 while dividing by 1 is 0. you will get same result if you doe <code>false%1</code></P>
241+
<P><strong>Answer:</strong> 0. When you are trying to find reminder of true, true becomes 1 and reminder of 1 while dividing by 1 is 0. You will get same result if you do <code>false%1</code></P>
242242
<P><strong>Question:</strong> <code>''%1</code></P>
243243
<P><strong>Answer:</strong> 0</P>
244244
</div>
@@ -248,17 +248,17 @@ <h2>5. Truthy isn't Equal to true</h2>
248248
<p><strong>Question: </strong> As <code>[]</code> is true, <code>[]==true</code> should also be true. right?</p>
249249
<p><strong>Answer:</strong> You are right about first part, <code>[]</code>, empty array is an object and object is always truthy. Hence, if you use <code>if([]){console.log('its true')}</code> you will see the log.</p>
250250
<p>However, special case about <code>==</code> (double equal) is that it will do some implicit coercion.</p>
251-
<p>Since left and right side of the equality are two different types, JavaScript can't compare them directly . Hence, under the hood, JavaScript will convert them to compare. first right side of the equality will be cooereced to a number and number of <code>true</code> would be 1.</p>
252-
<p>After that, JavaScript implementation will try to convert <code>[]</code> by using<code>toPrimitive</code> (of JavaScript implementation). since <code>[].valueOf</code> is not primitive will use <code>toString</code> and will get <code>""</code></p>
253-
<p> Now you are comparing "" == 1 and still left and right is not same type. Hence left side will be converted again to a number and empty string will be 0.</p>
254-
<p>Finally, they are of same type, you are comparing <code>0 === 1</code> which will be false.</p>
251+
<p>Since the left and right sides of the equality are two different types, JavaScript can't compare them directly. Hence, under the hood, JavaScript will convert them to compare. First, the right side of the equality will be cooereced to a number and number of <code>true</code> would be 1.</p>
252+
<p>After that, JavaScript implementation will try to convert <code>[]</code> by using<code>toPrimitive</code> (of JavaScript implementation). Since <code>[].valueOf</code> is not primitive, it will use <code>toString</code> and will get <code>""</code></p>
253+
<p> Now you are comparing "" == 1 and still, the left and right are not the same type. Hence, the left side will be converted again to a number and empty string will be 0.</p>
254+
<p>Finally, they are of same type. You are comparing <code>0 === 1</code> which will be false.</p>
255255
<p>ref: <a href="http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/">angus croll: truth and eqality in JS</a>, ref: <a href="http://www.sitepoint.com/javascript-truthy-falsy/">truthy and falsy</a></p>
256256
</div>
257257

258258
</div>
259259
<div id="extendObject">
260260
<h2>6. Extend Core Object</h2>
261-
<p><strong>Question:</strong> How could you write a method on instance of a date which will give you next day?</p>
261+
<p><strong>Question:</strong> How could you write a method on an instance of a date which will give you the next day?</p>
262262
<p><strong>Answer:</strong> I have to declare a method on the prototype of Date object. To get access to the current value of the instance of the date, i will use <code>this</code></p>
263263
<pre><code>
264264
Date.prototype.nextDay = function(){

0 commit comments

Comments
 (0)