Split First name and Last name using JavaScript?

Web DevelopmentJavascriptObject Oriented Programming

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
raja
Published on 12-Sep-2020 11:38:42
Advertisements