Comments (16)
Comment written by Azizul Haque on 06/17/2017 17:28:27
This article explains exactly what I was looking for. Thanks for nice explanation. Can you please explain why {} + 15 = 15 instead "[object Object]15" where 15 + {} = "15[object Object]" ?
from dmitripavlutin.com-comments.
Comment written by Dmitri Pavlutin on 06/19/2017 15:48:34
I'm glad you find the article helpful!
{} + 15 = 15 is 15 because JavaScript interprets { } as an empty block statement followed by +15. This construction is equivalent to:
{
// empty block
}
+ 15
You can add a pair of parenthesis to explicitly define an object literal: ( {} ) + 15="[object Object]15".
The second case 15 + {}. JavaScript first transforms the object literal into a string, then transforms 15 into a string and concatenates: "15[object Object]".
from dmitripavlutin.com-comments.
Comment written by Azizul Haque on 06/19/2017 16:10:12
Thank you very much for your excellent explanation. Can you also do me a favour explaining why do JavaScript sometimes interprets {} as empty block and sometimes convert it into a string?
Thanks again
from dmitripavlutin.com-comments.
Comment written by Dmitri Pavlutin on 06/19/2017 16:17:40
Check here some details.
from dmitripavlutin.com-comments.
Comment written by luckyluckyx x on 06/25/2017 03:06:23
I learn a lot , thank you!
from dmitripavlutin.com-comments.
Comment written by Artur Mostowiak on 02/01/2018 20:40:26
This is a good explanation. Good job and thanks :)
from dmitripavlutin.com-comments.
Comment written by SAYED HASAN on 02/21/2018 04:51:38
Thanks dude
from dmitripavlutin.com-comments.
Comment written by Timur on 08/05/2018 08:30:56
Example 1: Number and string:
var result = 1 + "5"; // "15"
Explanation:
1 + "1" (The second operand is a string and based on rule 2 the number 1 becomes "1") // 1 + "5" ?
"1" + "1" (Strings concatenation) // "1" + "5" ?
"15"
from dmitripavlutin.com-comments.
Comment written by Tomasz Dudek on 07/06/2019 11:48:17
I study your articles (they great, the best I could found) but I still cannot understand how JS converts types. For example:
null + 8 // 8
because null is converted into 0
but
null == 0 // false
null is not converted... why?
Is types conversion dependent on operators?
from dmitripavlutin.com-comments.
Comment written by Dmitri Pavlutin on 07/07/2019 08:57:05
Thanks for reading my articles. I'm glad you find them useful.
`null == 0` is false because null is loosely equal == only to null or undefined.
Addition and equality are rather different operators, that's why the conversion rules are different.
Anyways, I always avoid comparing values of different types. That's the best rule to avoid misunderstandings.
from dmitripavlutin.com-comments.
If switch the operands in example 4 and do
{} + 15 // this returns 15 (the number!)
Why is that?
from dmitripavlutin.com-comments.
If switch the operands in example 4 and do
{} + 15 // this returns 15 (the number!)
Why is that?
See this comment.
from dmitripavlutin.com-comments.
1+2+"3" gives 33
while "1"+2+3 gives 123 why is that??
from dmitripavlutin.com-comments.
1+2+"3" gives 33
while "1"+2+3 gives 123 why is that??
What are your considerations on the question?
from dmitripavlutin.com-comments.
Great article, I recently encountered some code on leetcode that peaked my interest in how JavaScript handles the + and - operators when mixed with string numeric. The challenge was to replicate the atoi method. This idea was copied from a C++ example so the principles seem to work across multiple languages. I still have yet to find a clear description on how this works
const ar = ['9'];
let i = 0;
let result = 51;
let interim = result * 10 + (ar[i]); // 5109
result = result * 10 + (ar[i] - '0'); // 519
console.log(`\tcalculating ${interim} - '0' = ${result}`);
from dmitripavlutin.com-comments.
Interesting Article!
One more questions:
Since []+3 = 3 because the objects will turn into strings (''+3='3')
if I use an object literal as follows, why the result is a numeric value instead a string
{}+3 =3.
Great Blog BTW!
from dmitripavlutin.com-comments.
Related Issues (20)
- react-usememo-hook/ HOT 12
- javascript-json-modules/ HOT 7
- javascript-import-meta/ HOT 3
- javascript-array-group/ HOT 16
- javascript-array-flatmap/ HOT 23
- interface-vs-implementation/ HOT 11
- vue-next-tick/ HOT 4
- props-destructure-vue-composition/ HOT 7
- ref-reactive-differences-vue/ HOT 5
- environment-variables-javascript/ HOT 3
- ref-in-vue/ HOT 1
- typescript-function-type/ HOT 5
- vue-v-model-form-inputs/ HOT 2
- react-forwardref/ HOT 4
- javascript-enum/ HOT 19
- react-comments/ HOT 8
- typescript-record/ HOT 10
- screen-window-page-sizes/ HOT 3
- javascript-pure-function/ HOT 1
- Great article
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dmitripavlutin.com-comments.