Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript how to get middle letters of a string

 function extractMiddle(str) {

        var position;
        var length;

        if(str.length % 2 == 1) {
            position = str.length / 2;
            length = 1;
        } else {
            position = str.length / 2 - 1;
            length = 2;
        }

        return str.substring(position, position + length)
    }

    console.log(extractMiddle("handbananna"));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #middle #letters #string
ADD COMMENT
Topic
Name
1+9 =