Q:

In JavaScript why Number() function does not work here and gives back a string?

I have this simple example and can not figure out the solution. Can somebody please tell me why n is still a string after used the Number() function? Thanks!
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Still a string</title>
<style>
  input {
    width: 30px;
  }
</style>
</head>
<body> 
    n = <input type="number" id="n"><br/>

    <button id="but" onclick="gen()">Push me!</button>

    <script>
        var n;
        function gen() {
        n = document.getElementById("n").value;
        Number(n);
        alert(typeof n);
        }
    </script>
</body>
</html>

A:

The primitive data types (Strings, numbers, boolean etc.) in javascript are immutable
So, whenever you change String "n" to number, a number is stored on memory but "n" is still referring to the same String.
Instead you should type your code as:
var n;
function gen() {
    n = document.getElementById("n").value;
    n = Number(n);
    alert(typeof n);
}

