Split First name and Last name using JavaScript?


Let’s say the following is our string with name −

var studentFullName="John Smith";

Use split() to split the first name and last name. Following is the code −

Example

var studentFullName="John Smith";
var details=[]
var details=studentFullName.split(' ');
console.log("StudentFirstName="+details[0])
console.log("StudentLastName="+details[1]);

To run the above program, you need to use the following command −

node fileName.js.

Output

Here, my file name is demo163.js. This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo163.js
StudentFirstName=John
StudentLastName=Smith

Updated on: 12-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements