Kristen P. answered 06/20/19
Experienced National Merit (PSAT/SAT) tutor / multi-subject
Commas should be omitted if the "such as" phrase is essential to the meaning of the sentence. An example would be "Fleet, four-legged animals such as deer and antelope have hooves to withstand the force of their rapid movement". Without the phrase initiated by 'such as', the sentence would not mean the same thing, and would be untrue (think of dogs, which are fleet and four legged but do not have hooves). It would not be true to say "Fleet, four-legged animals have hooves ...." because so many very fast animals with four legs do not have hooves, so removing the such as clause would alter the meaning of the sentence to make it untrue. Therefore, the 'such as' phrase is essential to the meaning of the sentence. This rule refers to what are called "restrictive" phrases, meaning that the phrase is restricting the meaning of the sentence.
On the other hand, if the phrase introduced by the 'such as' is not essential to the meaning of the sentence (referred to technically as 'non-restrictive' phrasing),then commas should be used. For example... "Some flavors of ice cream, such as strawberry and blueberry, are fruity". In this case the sentence would mean exactly the same thing without providing examples. Therefore, the phrase is non-restrictive, working simply to elaborate on something that is already clear from the rest of the sentence.