Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

310
Vistas
How to get first and last name when someone enters full name in Javascript?

How to get someone's first and last name when they enter their full name if someone's first and/or last name is more than 1 word each? Like for example "Juan García Reyes" where "García Reyes" is the last name, or "Anne Marie Smith" where Anne Marie is the first name.? I have this:

set fullName(value) {
  const parts = value.split(" ");
  this.firstName = parts[?];
  this.lastName = parts[?]
}

or this:

set fullName(value) {
    [this.firstName, this.lastName] = value.split(' ');
  }

but they only work when the first and last name are both only 1 word. I'm a beginner so please forgive me if this is a bit stupid!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First read this

https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

I do not recommend it, but you can do

const value = "John John James";
const parts = value.split(" ");
const lastName = parts.pop()
const firstName = parts.join(" ")

console.log(lastName)
console.log(firstName)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You cannot guess what kind of data your user will inform, they can provide either one, two or three words, being the firstname the first two or only the first one, for example. But you can make them inform the way you want. Try setting meaningful inputs, one for first name and another one for last name.

about 4 years ago · Juan Pablo Isaza Denunciar

0

make two seperate inputs and let the user write their first name in one and last name in the other. javascript has no ability to detect a first/last name and writing code for it seems crazy when you can just have two seperate inputs

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda