JavaScript: Extract the first half of a string of even length
JavaScript Basic: Exercise-59 with Solution
Extract First Half of Even-Length String
Write a JavaScript program to extract the first half of a even string.
This JavaScript program takes an input string with an even number of characters and returns a new string containing only the first half of the original string.
Visual Presentation:

Sample Solution:
JavaScript Code:
Output:
Pyt JavaS PHP
Live Demo:
Flowchart:

ES6 Version:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that checks if a string has an even number of characters and returns its first half, otherwise returns an error message.
- Write a JavaScript function that trims whitespace from an even-length string and then extracts its first half.
- Write a JavaScript program that verifies if a string is even in length and extracts the first half using substring methods.
Go to:
PREV : Four Copies of Last 3 Characters.
NEXT : Remove First and Last Characters in String.
Improve this sample solution and post your code through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.