window.onload = randomCharacter; function randomCharacter(){ var hexstr = Math.floor(Math.random()*9999).toString(16); hexstr = '0000'.subStr(hexstr.length)+hexstr; alert('"\\u'+hexstr+'"'); }