Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:44:41+00:00 2026-06-01T20:44:41+00:00

When writing JavaScript I always took for granted that adding two integers together resulted

  • 0

When writing JavaScript I always took for granted that adding two integers together resulted in another integer. Or that adding two strings together would result in the concatenation. But I got to thinking, how does the language determine the types of instances behind the scenes prior to performing operations using those instances?

var one = 1;
var two = 2;
var fourStr = 'four';
var floaty = 1.5;

//this results in an integer
var three = one + two;           //3

//but this results in a string
var result = fourStr + one;      //'four1'

//and this results in a float
var floatenized = one + floaty;  //2.5

Does the runtime just determine the instance types and then reference some sort of internal type precedence or something? Can anyone explain exactly how these operations are performed by the runtime when instances of differing types are combined?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:44:43+00:00Added an answer on June 1, 2026 at 8:44 pm

    The ecmascript specification describes exactly how operators like + (section 11.6.1) act with different types.

    Relevant for your example:

    • (number) + (number): result will be the sum of the floats
    • (string) + (number): result will be the concatenation of the string and the string represantation of the number
    • (number) + (string): result will be the concatenation of the string represantation of the number and the string

    But the + can also act as a unary operator to transform a string into a number, as like as the binary - operator does (e.g.: +"4"+5==9, 5-"4"==1).

    The type conversion algorithms are described in section 9 of the spec. How an environment should store the types of primitives and objects is not specified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Javascript function that would manipulate an array written on-the-fly and sent
As I'm writing JavaScript I'm always missing some fairly basic language features that JavaScript
I'm writing a JavaSCript class that has a method that recursively calls itself. Scheduler.prototype.updateTimer
So I'm writing a Javascript library that takes x,y coordinates and uses them to
I'm writing some Javascript code that generates an XML document in the client (via
I am writing this in JavaScript, and the issue is that I can't seem
I've always been dabbling in javascript, but never really got hugely into writing a
A problem I regularly come across writing javascript using jQuery is that when a
I would like to prepare a javascript-based system that would deselect the text after
I'm writing a small Chrome extension that would have a content_script . It would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.