Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get middle of string js

const getMiddle = s => {
  let indexStart;
  let indexEnd;
  if (s.length % 2 > 0) {
    indexStart = Math.floor(s.length / 2);
    indexEnd = indexStart + 1;
  } else {
    indexStart = (s.length / 2) - 1;
    indexEnd = indexStart + 2;
  }
  return s.substring(indexStart, indexEnd);
};
 
PREVIOUS NEXT
Tagged: #middle #string #js
ADD COMMENT
Topic
Name
2+3 =