Extract only the first character:slice(0,1) - Javascript String

Javascript examples for String:slice

Description

Extract only the first character:slice(0,1)

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w w  w. ja  v  a 2  s. c  o  m
    var str = "Hello world!";
    var res = str.slice(0,1);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials