• Home
  • About Us
  • GDPR Policy
  • Disclaimer
  • Privacy Policy
  • Contact Us
bdma
  • Business
  • Knowledge
    • Google Adsense
    • Visa
  • Sports
  • Travel
  • Technology
  • Trending
  • Entertainment
    • Bollywood
No Result
View All Result
  • Business
  • Knowledge
    • Google Adsense
    • Visa
  • Sports
  • Travel
  • Technology
  • Trending
  • Entertainment
    • Bollywood
No Result
View All Result
bdma
No Result
View All Result

JavaScript Is Weird. And That’s Why We Love It.

bdma by bdma
August 25, 2023
in Technology
0

(This story is an original creation generated automatically from the Syndicated News feed. The content body has not been modified or edited by the BDMA Team.)

JavaScript Is Weird. And That’s Why We Love It.

According to the 2021 Stack Overflow Developer Survey, JavaScript is the most widely used technology among professionals and amateurs alike.

Some 61.51% of developers love the coding language. But that still leaves some 40% of programmers who prefer to stay away. So why is that? Well, there’s a fair degree of weirdness about JS, which could be one explanation.

Just check the example below:

 

JavaScript

 

In this article, we’ll run through why some JavaScript actions work the way they do (as opposed to how you might expect).

No hanging around, let’s get stuck in!

Table of Contents

  • The oddities of Numbers vs Not a Number
  • What’s wrong with the Object and Array?
  • The truth, the whole truth, but is it really the truth?

The oddities of Numbers vs Not a Number

Let’s start by looking at typeof NaN.

Many people ask why the entry to this operation is number. You can read a lot about this topic in the specifications, which state what numbers mean in JS:

‘Number type set of all possible Number values including the special “Not-a-Number” (NaN) value, positive infinity, and negative infinity’

But what exactly is “Not-a-Number”? In simple terms, NaN is something that tries to be a number — but fails.

An example could be something that:

    • Goes beyond the range of numbers supported by JavaScript
    • Tries to convert a string to a number, e.g., parseInt ('meow')
  • Divides zero by zero
  • Multiplies zero times infinity

And there are several more cases where the result cannot be determined by the actual numbers in the range supported by JS.

 

9999999999999999 === 10000000000000000

 

Once again, the result seems odd — so where does this bizarreness come from? The answer is similar to the previous case.

JavaScript uses floating-point numbers written using the IEEE 754 standard, which (like every number standard) has its limitations. In essence, when you pass a certain limit, natural numbers (integers) are no longer exact. You may now be asking, “So, is there a way to check this border?” 

Again, the standard JS library gives two constants within which we can safely perform calculations.

 

Number.MAX_SAFE_INTEGER < 9999999999999999
Number.MIN_SAFE_INTEGER > -9999999999999999

 

In the example, you can see that going beyond them isn’t a great idea if you want accurate calculations.

 

0.2 + 0.1 !== 0.3 but 0.5 + 0.1 === 0.6

 

Here’s another quirk of JavaScript that seems super weird. But in actual fact, this is not so much the fault of the language. It’s more a problem with how numbers are written in binary. Fortunately, there is a way to deal with this. 

You just need to enter results with a reasonable number of decimal places.

 

Math.max() === -Infinity and Math.min() === Infinity

 

This takes us onto the real question: what are these two functions? Their task is to select the largest and smallest number from a given set of numbers as arguments in turn.

What’s wrong with the Object and Array?

 

[] + [] === “”

 

And you guessed it — now we enter even more weirdness. If you recall your first JavaScript course, tutorial, or book, we bet it said something like ‘JS is a dynamically typed language’. But what does that mean in practice? 

In short, it means we shouldn’t worry about the type of variable. We should only assign a value to it, then let the JS engine determine the type we are dealing with.

 

let a = 124;
let b = 'meow';
typeof a // number
typeof b // string

 

How does JS do this? By using something called the “duck typing” mechanism. The following quote paints a nice picture of how this works:

‘If it looks like a duck and quacks like a duck, it must be a duck.’

OK — nice quote: but how does it relate to the example? 

Well, it just so happens that arrays in JS cannot be added together with the + operator. However, we can combine two text strings this way. Meaning a smart JS engine can project arrays onto empty strings dynamically, then join them together. And the output is an empty string.

 

[] + {} === ‘[Object object]’

 

Again, the result may surprise you. But the principle actually reflects the previous example. As we know, an array cannot be added to something, so it is converted to an empty string. As a result, the object is also converted to a string.

The following example is what the textual representation of an object might look like:

 

 String({})

 

{} + [] === 0

 

OK, if you’ve managed to stick with us until now, you’re doing great. But the next part could be the final straw. Because you might now be wondering, ‘But why is the addition not commutative?’ When, in actual fact, it is! 

Just remember that previously, we did not add. Instead, we combined (concatenated) two strings of characters. ‘So are we adding now?’ you ask. Well, of course not! That would be too easy. This plus sign changes the empty array to a number here.

.

+ '27' // 27
!! 1   // true
'' + 777 // '777'


If you didn’t know the magic of these methods, now you do. And we’re sure you’ll find them helpful to avoid having to write casting functions.

Still, you might feel there’s a little weirdness lurking somewhere. And that’s because we still have this empty object. But, surprise, surprise, JS has tricked you once again! It just so happens that {} is not only used to signify an empty object.

Sometimes, it’s used to declare a block of code. And at this point, the parser treats it as an empty code block that does absolutely nothing. 

Consequently, it’s ignored for this operation.

The truth, the whole truth, but is it really the truth?

 

true + true + true === 3 and true-true === 0

 

As is the case with most programming languages, true is equated with the number 1. Therefore, no one should be surprised that 3 * true gives us 3. And the same goes for `1 – 1 === 0.

 

true == 1 but true !== 1

 

Here, you should understand the difference between the ‘equality’ == and ‘identity’ === operators. The identity operator does not allow implicit type coercion. And as a result, true (which is a boolean type) cannot become 1 (a number).

 

(!+[]+[]+![]).length === 9

 

That might have your head in a bit of spin, so to cheer you up, know that only a few things will ever truly surprise you in programming. But to understand this, you have to break the parenthetical expression into pieces once again.

It will be easier to explain what mechanisms take place here if we consider this expression from the end, so let’s do that:

  • ![] – gives false. Why? An array is really an object, not a simple type. Any object cast to boolean returns true. Hence, the denial of true gives us false.
  • []+![] is the same as []+false. It converts an empty array to an empty string. The result of this expression is, therefore, 'false' as a string.
  • !+[] – a + here doesn’t mean addition, but casting the array to a number, so the result will be 0, and if we deny zero, we’ll get true.
  • This way, we arrived at the most simplified version of the parenthesis. It looks like this: (true + 'false'), which results in string concatenation, i.e. 'truefalse'. The length of the starting string is 9.

 

Now we’ve been through much of the weirdness of JavaScript, we hope you’re learning to love it as much as we do. And if you feel we’ve missed something — or if you have a question for the team — feel free to share your thoughts on Twitter or LinkedIn.

 

______

This article was originally published in Polish on my personal blog.

Previous Post

A date with Neymar and Al Hilal: Mumbai City hopes to cherish opportunity in AFC Champions League

Next Post

Pulisic looking forward to playing at San Siro for AC Milan after dream debut

Next Post
Pulisic looking forward to playing at San Siro for AC Milan after dream debut

Pulisic looking forward to playing at San Siro for AC Milan after dream debut

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • All
  • Sports
Crumly & Associates Inc. Increases Stake in Boston Scientific Co.

Crumly & Associates Inc. Increases Stake in Boston Scientific Co.

by bdma
October 3, 2023
0

Crumly & Associates Inc. has raised its stake in Boston Scientific Co. by 48.9% during the second quarter, according to...

FIFA moved to block potential witness tampering by Luis Rubiales amid sexual assault scandal

FIFA moved to block potential witness tampering by Luis Rubiales amid sexual assault scandal

by bdma
October 3, 2023
0

FIFA moved to block potential witness tampering by Luis Rubiales when its disciplinary committee suspended the now-ousted Spanish football president,...

Chainalysis Cuts 150 Jobs as It Shifts Focus to Government Contracts

Chainalysis Cuts 150 Jobs as It Shifts Focus to Government Contracts

by bdma
October 3, 2023
0

Chainalysis, a leading provider of blockchain analysis, is laying off approximately 150 employees, which accounts for more than 15% of...

Premier League: Mudryk, Broja score as Chelsea beats Fulham 2-0

Premier League: Mudryk, Broja score as Chelsea beats Fulham 2-0

by bdma
October 3, 2023
0

Chelsea’s long wait for another Premier League win came to an end on Monday with a 2-0 victory at Fulham.Two...

Jones Lang LaSalle (JLL) Q4 2023 Earnings Estimates Cut by Zacks Research

Canadian Banks Face Headwinds Due to Macroeconomic Conditions

by bdma
October 3, 2023
0

The current macroeconomic environment presents a challenging landscape for Canadian banks, according to Bank of America (BofA) analyst Ebrahim Poonawala....

VAR officials involved in Liverpool error not selected for weekend games

VAR officials involved in Liverpool error not selected for weekend games

by bdma
October 3, 2023
0

The Video Assistant Referee (VAR) officials involved when Liverpool was wrongly denied a goal in Saturday’s 2-1 Premier League defeat...

The History and Background of Bellerophon Therapeutics Inc

Asahi Life Asset Management Adds Stake in Paycom Software

by bdma
October 3, 2023
0

Asahi Life Asset Management CO. LTD. has purchased a new stake in Paycom Software, Inc. during the second quarter, according...

Champions League 2023/24: 7-0 thrashing ‘history’ says coach Rose as Leipzig seek revenge against City

Champions League 2023/24: 7-0 thrashing ‘history’ says coach Rose as Leipzig seek revenge against City

by bdma
October 3, 2023
0

RB Leipzig coach Marco Rose said last season’s 7-0 thrashing by Manchester City was “history” as the two sides prepare...

Philip Morris International Inc.: Is it Undervalued?

Philip Morris International Inc.: Is it Undervalued?

by bdma
October 3, 2023
0

In this article, we will assess the intrinsic value of Philip Morris International Inc. (NYSE:PM) using the 2-stage Free Cash...

Brazil replaces injured Raphinha with David Neres for World Cup qualifying

Brazil replaces injured Raphinha with David Neres for World Cup qualifying

by bdma
October 3, 2023
0

Brazil winger Raphinha won’t play in the next two rounds of South American World Cup qualifying because of injury.The Brazilian...

Arsenal welcomes midfielder Partey back ahead of Lens clash

Arsenal welcomes midfielder Partey back ahead of Lens clash

by bdma
October 3, 2023
0

Arsenal midfielder Thomas Partey will be included in the squad to play at Lens in the Champions League on Tuesday...

DAVENPORT & Co LLC Increases Stake in Global Payments Inc.

DAVENPORT & Co LLC Increases Stake in Global Payments Inc.

by bdma
October 3, 2023
0

DAVENPORT & Co LLC has increased its stake in Global Payments Inc. by 74.2% during the second quarter, according to...

Football this week (September 25-October 1): Cristiano Ronaldo continues goalscoring, Sunil Chhetri and Co. out of Asian Games

Football this week (September 25-October 1): Cristiano Ronaldo continues goalscoring, Sunil Chhetri and Co. out of Asian Games

by bdma
October 3, 2023
0

Match of the week: Tottenham Hotspur vs LiverpoolTottenham Hotspur and Liverpool played out what could turn out to be the...

Tuchel unfazed by Bayern’s inconsistent form ahead of Copenhagen clash

Tuchel unfazed by Bayern’s inconsistent form ahead of Copenhagen clash

by bdma
October 3, 2023
0

Bayern Munich will continue to search for consistency but is not worrying too much about visiting FC Copenhagen in Tuesday’s...

UEFA Champions League: Rebuilt RB Leipzig ready for Manchester City test

UEFA Champions League: Rebuilt RB Leipzig ready for Manchester City test

by bdma
October 3, 2023
0

RB Leipzig faces Champions League title holder Manchester City on Wednesday, having emerged strongly from a summer rebuild that could...

Lampard ‘not completely surprised’ by Chelsea’s struggles

Lampard ‘not completely surprised’ by Chelsea’s struggles

by bdma
October 3, 2023
0

Former Chelsea manager Frank Lampard said the Premier League club’s struggles this season have not taken him by surprise.Former Tottenham...

The Rise of Black Spade Acquisition Co.: A Success Story

Indian Startups Witness Varied Trends in Q3 2023

by bdma
October 3, 2023
0

The third quarter of 2023 was marked by several noteworthy developments in the Indian startup ecosystem. Funding trends showcased a...

Champions League: Porto chance for Barca to prove progress in Xavi’s 100th game

Champions League: Porto chance for Barca to prove progress in Xavi’s 100th game

by bdma
October 3, 2023
0

After consecutive premature and humiliating Champions League exits, Barcelona visits Porto on Wednesday aiming to make a statement in its...

UEFA Champions League: Manchester United plays Galatasaray as UCL back at Old Trafford; Napoli hosts Madrid

UEFA Champions League: Manchester United plays Galatasaray as UCL back at Old Trafford; Napoli hosts Madrid

by bdma
October 3, 2023
0

A look at what’s happening in European football on Tuesday:CHAMPIONS LEAGUEManchester United vs. Galatasaray (Group A)The Champions League is back...

Check Point Software Technologies Ltd

Rising Macro Challenges for Emerging Markets

by bdma
October 3, 2023
0

Economists are warning that emerging markets, including India, may face significant macroeconomic challenges due to rising crude oil prices, a...

Pochettino’s patience pays off as young guns fire Chelsea to victory

Pochettino’s patience pays off as young guns fire Chelsea to victory

by bdma
October 3, 2023
0

Chelsea boss Mauricio Pochettino said he never lost faith in his talented young players and that it was important they...

  • Business
  • Knowledge
  • Sports
  • Travel
  • Technology
  • Trending
  • Entertainment

No Result
View All Result
  • About Us
  • Contact Us
  • Disclaimer
  • GDPR Policy
  • Home
  • Privacy Policy