Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
When adding number and string type input, output is giving concatenation but string and number giving strange output in javascript?

I know that addition of number and string concatenates the two inputs in javascript which is completely understandable but string and number addition giving strange output. Please somebody let me know why this is happening.

calculate.js

let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split("\n");

function readLine() {
    idx++;
    return data[idx - 1];
}

let unitCost = readLine();
console.log(unitCost);
console.log(typeof(unitCost))

let GSTpcnt = readLine();
console.log(GSTpcnt);
console.log(typeof(GSTpcnt));


let tax = GSTpcnt / 100 * unitCost; 
console.log(tax);
console.log(typeof(tax));


// let finalCost = tax + unitCost; // number + string ==> 18100
let finalCost = unitCost + tax; //string + number ==>  180

console.log(finalCost);
console.log(typeof(finalCost));

input.txt:

100
18
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!