loader image

JavaScript program to check palindrome string

Write a JavaScript program to check whether a given string is palindrome or not.

<html>

<body>
    <h1>Palindrome</h1>
    Enter String: <input type="text" id="txt" />
    <input type="button" value="Check" onClick=checkPalindrome()/>
    <p id="result"></p>

<script>
    function checkPalindrome() {
        var str = document.getElementById('txt').value;
        const len = str.length;
        var flag = 1;
        for (var i = 0; i < len / 2; i++) {
            if (str[i] !== str[len - 1 - i]) {
                flag = 0;
            }
        }
        if (flag == 1) {
            document.getElementById("result").innerHTML = "It is a palindrome";
        }
        else {
            document.getElementById("result").innerHTML = "It is not a palindrome";
        }

    }
</script>
</body>
</html>
Output
palindrome js
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top