Stars display without JavaScript
HTML
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<b>Rating: <span id=stars></span></b
JavaScript
document.getElementById("stars").innerHTML = getStars(3.6);
function getStars(rating) {
// Round to nearest half
rating = Math.round(rating * 2) / 2;
let output = [];
// Append all the filled whole stars
for (var i = rating; i >= 1; i--)
output.push('<i class="fa fa-star" aria-hidden="true" style="color: gold;"></i> ');
// If there is a half a star, append it
if (i == .5) output.push('<i class="fa fa-star-half-o" aria-hidden="true" style="color: gold;"></i> ');
// Fill the empty stars
for (let i = (5 - rating); i >= 1; i--)
output.push('<i class="fa fa-star-o" aria-hidden="true" style="color: gold;"></i> ');
return output.join('');
}
return output + '</div>';