2 Example(s) of JavaScript reverse function


Description :

JavaScript reverse function is used to reverse the array. arr.reverse() will reverse the array values. See the code snippet:


JavaScript reverse function Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<script>
   var arr= ['abc', 'xyz', 'def'];
   arr.reverse();
   document.write(arr);
</script>
</body>
</html>

Output

Description :

In this example, JavaScript reverse function will reverse the string. string 'I love learnkode.com' will be changed to "moc.edoknrael evol I". See the code snippet:


JavaScript reverse function Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<script>
var text = 'I love learnkode.com';
var result= text.split('').reverse().join('');
    document.write(result);
</script>
</body>
</html>

Output