• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

JavaScript: Why does 3 + true = 4? (and 7 other tricky equations)

  • Arif Khoja
  • July 16, 2017
  • 2 minute read
JS
Total
0
Shares
0
0
0

How to Follow Along

It’s about to get weird — and you’re going to want to follow along with me. You can open up your Chrome Developer Console with: (Windows: Ctrl + Shift + J)(Mac: Cmd + Option + J). This will allow you to type all of the following code into your browser so you can see in real time what is happening…

3 + true == 4

I’m not kidding either. In JavaScript, when the plus operator is placed between a number and a boolean, the boolean is converted to a number.

If you know here  false == 0 and true == 1. With this in mind, 3 + true is converted to 3 + 1 and thus we get the answer of 4.

true + false

This follows the same logic as the above example. When the plus operator is placed between two booleans, the booleans are converted to numbers. Thus, true + false is converted to 1 + 0 and we get an answer of 1.

‘4’ + 8

What happens when we add a string number to an actual number? When the plus operator is placed between to operands, and one is a string, it will convert the other number or boolean to a string and concatenate them.

By this logic: ‘4’ + 8 becomes ‘4’ + ‘8’ and we get an answer of ’48’.

true + ‘4’

Similar to the above example, JavaScript will convert the boolean into a string value and concatenate. This becomes ‘true’ + ‘4’ and the result is ‘true4’.

1 + 1 + ‘1’

Order of operations is important. In this instance, JavaScript evaluates the first + before anything else: 1 + 1 equals 2. Then we move on and add in the string value of ‘1’. Concatenation occurs and the result is ’21’.

Here’s the chain of events:

1 + 1 + '1'
  2   + '1'
       '21'

‘1’ + 1 + 1

What changes when we have our string value first instead of last? Remember the order of operations and work from left to right:

'1' + 1 + 1
   '11' + 1
       '111'

string + number = string. So in this instance we’re left with one long string of ‘111’.

-‘69’ + 69

What if we attempt to negate a string and then add a number? As you should know by now, without the negation, our answer would be ‘6969’. However, the negation changes things.

The minus sign before ’69’ is a unary minus operator that will actually convert the string to a number and make it negative. Thus, our equation becomes -69 + 69 which equals 0.

-‘giddyup’ + 409

What if our unary minus operator is in front of a string that can’t be converted to a number? When JavaScript fails to produce a number, we are left with NaN (Not A Number).

You did it.

Good work!I hope you enjoyed my article. I publish a few articles and tutorials each week, please enter your email below if you’d like to be added to my once-weekly email list.

 

Arif Khoja
Arif Khoja

Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.

Views: 5,267

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
1 comment
  1. Pingback: Debugging With Black Box - Javascript JavaScript - Code4Developers

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d