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

251
Views
In javascript, How to check if the last element of an array is a number or not. Note that the last element can have either number or string depends

console.log("case 1")
var event = "Year 2021";
console.log(typeof(parseInt(event.split(" ").pop())) === "number");
console.log("case 2")
var event = "Year mukesh";
console.log(typeof(parseInt(event.split(" ").pop())) === "number");
console.log("case 3")
var event = "Year mukesh";
console.log(typeof(event.split(" ").pop()) === "number");
console.log("case 4")
var event = "Year 2021";
console.log(typeof(event.split(" ").pop()) === "number");

case 1 when we have a proper number in last place with using parseInt gives true and that is legit!

case 2 when we have a string in last place and still using parseInt...should give false but because of parseInt it gives us valid number type

case 3 when we have a string in last place without using parseInt... gives false that too is legit!

case 4 when we have a number in last place without using parseInt.. gives false that is too is legit! well because "2021" is a string because 2021 is indeed enclosed within " ".

Now how do I check if the last element of the array is a string or an integer because from the user I could receive any combination either a "Hello darkness" or "Hello 221".

Check on jsfiddle for more clarification

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use isNaN to determine whether a value is NaN or not:

console.log("case 1")
var event = "Year 2021";
console.log('Is a number?', !isNaN(event.split(" ").pop()));

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!