Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why not give error when I change variable name?

I am trying to make a calculator but javascript file shows error.

var top = document.getElementById('top');
var bottom = document.getElementById('bottom');



function calculator(value) {
    if (value === 'AC') {
        top.innerText = (top.innerText).slice(0, -1);
    } else if (value === '=') {
        top.innerText = ''
    } else {
        top.innerText += value;
    }
    bottom.innerText = `${eval(((top.innerText)).replace('%', '/100').replace('x', '*'))}`;
    if (bottom.innerText === 'undefined') {
        bottom.innerText = 0;
    }

}
button {
    border-radius: 10px;
    margin-top: 5px;
    font-size: 200%;
    width: 6%;
    background: #F7ECDE;
    border: 2px solid black;
    box-shadow: 0 2px #999;
    cursor: pointer;
}

button:active {
    box-shadow: 0 3px #666;
    transform: translateY(2px);
}

button:hover {
    background-color: rgba(0, 0, 0, 0.709);
    color: rgb(215, 254, 255);
    transition: background-color .5s;
}

.container {
    text-align: center;
}

.display {
    height: 75px;
    margin: auto;
    width: 24.6%;
    border: 2px solid black;
}

.top,
.bottom {
    background: rgb(219, 204, 204);
    padding-right: 5px;
    text-align: right;
}

.top {
    font-size: 100%;
    height: 25px;
}

.bottom {
    font-size: 200%;
    height: 50px;
    border-top: 1px solid black;
}

@media (max-width:950px) {
    button {
        font-size: 100%;
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    
</head>

<body>
    <div class="container">
        <div class="display">
            <div id="top" class="top"></div>
            <div id="bottom" class="bottom"></div>
        </div>
        <div><button onclick="calculator(this.innerText)">(</button>
            <button onclick="calculator(this.innerText)">)</button>
            <button onclick="calculator(this.innerText)">%</button>
            <button ondblclick="Top.innerText=''" class="AC" onclick="calculator(this.innerText)">AC</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)">7</button>
            <button onclick="calculator(this.innerText)">8</button>
            <button onclick="calculator(this.innerText)">9</button>
            <button on onclick="calculator(this.innerText)">/</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)">4</button>
            <button onclick="calculator(this.innerText)">5</button>
            <button onclick="calculator(this.innerText)">6</button>
            <button onclick="calculator(this.innerText)">x</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)">1</button>
            <button onclick="calculator(this.innerText)">2</button>
            <button onclick="calculator(this.innerText)">3</button>
            <button onclick="calculator(this.innerText)">-</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)" class="zero">0</button>
            <button onclick="calculator(this.innerText)">.</button>
            <button onclick="calculator(this.innerText)">=</button>
            <button onclick="calculator(this.innerText)">+</button>
        </div>
    </div>
    
    <script src="index.js"></script>
</body>

</html>

Error shows when I use this code But if I change change variable name top to anything else in javascript file it works perfectly

var Top = document.getElementById('top');
var bottom = document.getElementById('bottom');



function calculator(value) {
    if (value === 'AC') {
        Top.innerText = (Top.innerText).slice(0, -1);
    } else if (value === '=') {
        Top.innerText = ''
    } else {
        Top.innerText += value;
    }
    bottom.innerText = `${eval(((Top.innerText)).replace('%', '/100').replace('x', '*'))}`;
    if (bottom.innerText === 'undefined') {
        bottom.innerText = 0;
    }

}
button {
    border-radius: 10px;
    margin-top: 5px;
    font-size: 200%;
    width: 6%;
    background: #F7ECDE;
    border: 2px solid black;
    box-shadow: 0 2px #999;
    cursor: pointer;
}

button:active {
    box-shadow: 0 3px #666;
    transform: translateY(2px);
}

button:hover {
    background-color: rgba(0, 0, 0, 0.709);
    color: rgb(215, 254, 255);
    transition: background-color .5s;
}

.container {
    text-align: center;
}

.display {
    height: 75px;
    margin: auto;
    width: 24.6%;
    border: 2px solid black;
}

.top,
.bottom {
    background: rgb(219, 204, 204);
    padding-right: 5px;
    text-align: right;
}

.top {
    font-size: 100%;
    height: 25px;
}

.bottom {
    font-size: 200%;
    height: 50px;
    border-top: 1px solid black;
}

@media (max-width:950px) {
    button {
        font-size: 100%;
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/css/style.css">
    <script defer src="/js/index.js"></script>
</head>

<body>
    <div class="container">
        <div class="display">
            <div id="top" class="top"></div>
            <div id="bottom" class="bottom"></div>
        </div>
        <div><button onclick="calculator(this.innerText)">(</button>
            <button onclick="calculator(this.innerText)">)</button>
            <button onclick="calculator(this.innerText)">%</button>
            <button ondblclick="Top.innerText=''" class="AC" onclick="calculator(this.innerText)">AC</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)">7</button>
            <button onclick="calculator(this.innerText)">8</button>
            <button onclick="calculator(this.innerText)">9</button>
            <button on onclick="calculator(this.innerText)">/</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)">4</button>
            <button onclick="calculator(this.innerText)">5</button>
            <button onclick="calculator(this.innerText)">6</button>
            <button onclick="calculator(this.innerText)">x</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)">1</button>
            <button onclick="calculator(this.innerText)">2</button>
            <button onclick="calculator(this.innerText)">3</button>
            <button onclick="calculator(this.innerText)">-</button>
        </div>
        <div>
            <button onclick="calculator(this.innerText)" class="zero">0</button>
            <button onclick="calculator(this.innerText)">.</button>
            <button onclick="calculator(this.innerText)">=</button>
            <button onclick="calculator(this.innerText)">+</button>
        </div>
    </div>
</body>

</html>

can anyone Know why this happens.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because if you see in Reserved name in Javascript

top is also a reserved word hence, You can't use this as a variable name in javascript.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading