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

200
Views
JavaScript - How to get the line after the second comma

I have a certain line I want to get from this line the word "USA" which is after the second comma, how can I get it? I have a lot of such lines, I need to get a certain word that is after the second comma

let x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simply splitting the string on commas and accessing the element after the 2nd one should be enough. As long as the line does indeed contain enough commas you can do:

const x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
const result = x.split(",")[2].trim();

console.log(result);

Please note i called String.prototype.trim() function simply to remove whitespace from the beginning of a string.

about 4 years ago · Juan Pablo Isaza Report

0

You can split the string and get the second index from the value. Use optional chaining .? to avoid the error.

let x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
let value = x.split(',')?.[2]?.trim() || ""
console.log(value)

about 4 years ago · Juan Pablo Isaza Report
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!