You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js2.html
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -157,17 +157,17 @@ <h4>null</h4>
157
157
<divid="doubleVsTripleEqual">
158
158
<h2>2. == Vs ===</h2>
159
159
<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 typeto 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>
163
163
<ul>
164
164
<li>If both operands are same type use ===</li>
165
165
<li>undefined == null</li>
166
166
<li>If one operands is string another is number, convert string to number</li>
167
167
<li>If one is boolean and another is non-boolean, convert boolean to number and then perform comparison</li>
168
168
<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>
169
169
</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>
171
171
<pre><code>
172
172
var a = {a: 1};
173
173
var b = {a: 1};
@@ -185,7 +185,7 @@ <h2>2. == Vs ===</h2>
185
185
<h2>3. Object Equality</h2>
186
186
<p><strong>Question: </strong> How would you compare two objects in JavaScript?</p>
187
187
<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>
<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>
@@ -248,17 +248,17 @@ <h2>5. Truthy isn't Equal to true</h2>
248
248
<p><strong>Question: </strong> As <code>[]</code> is true, <code>[]==true</code> should also be true. right?</p>
249
249
<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>
250
250
<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 stillleft 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>
255
255
<p>ref: <ahref="http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/">angus croll: truth and eqality in JS</a>, ref: <ahref="http://www.sitepoint.com/javascript-truthy-falsy/">truthy and falsy</a></p>
256
256
</div>
257
257
258
258
</div>
259
259
<divid="extendObject">
260
260
<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>
262
262
<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>
0 commit comments