0

All,

Below is the code that I have written in JS.

<html>
   <head>
      <title>Fibonacci trial</title>
   </head>
   <body>
      <script type="text/javascript">
            var strRepresen = new String();
            var numberReprsen = new Number();
            strRepresen = "XXX";
            numberReprsen = 10;
            strRepresen = 5;
            numberReprsen = "XXX again";
            document.write(strRepresen);
            document.write(numberReprsen);
      </script>
   </body>
</html>

I have following queries:

1 > I am not able to understand how the line numberReprsen = "XXX again"; does not give an error or NaN when I have defined numberReprsen as a Number object.
2 > I have seen many scripts not writing <script type = "text/javascript">. Is it a standard or mandate to mention the script type?

Note: I am a beginner in Javascript.

1

3 Answers 3

2

I am not able to understand how the line numberReprsen = "XXX again"; does not give an error or NaN when I have defined numberReprsen as a Number object.

Variables in JavaScript are not typed. You have not defined numberReprsen as a Number variable. You have defined it as a variable. That it at one time happened to contain a Number object is irrelevant. You can store anything in it.

I have seen many scripts not writing <script type = "text/javascript">. Is it a standard or mandate to mention the script type?

I believe type is an attribute required by the more recent (X)HTML specifications. But yes, generally, it is a good idea to specify. I always do.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. So is it possible to check the type of a variable at any point ? something like isInteger() ?
Yes, but only in the most basic sense. For example, if you do var a = 1; then alert(typeof a); will display "number". But if you did var a = new Number(1); then alert(typeof a); will display "object" because the Number class essentially boxes a number in an object.
Oh, as a side note, there are specific techniques you can use to determine the type of an object, or if it is convertible to a type. For example, !isNaN(a) will determine if a is a number, a Number object, or a string that can be successfully converted to a number.
1

As far as your first question is concerned, Javascript is not a statically-typed language; it is a dynamically-typed language. Hence, types are associated with values and not with variables.

Initially numberReprsen is bound to a number, and later you are rebinding it to a string. Here, you're seeing exactly what I described; Javascript is typing the variable based on the value (which was initially a number, and then a string).

JavaScript has various ways to test the type of an object, one of which is duck typing.

To answer your second question, I think it is mandated by [X]HTML specs. Either way, I always specify a type attribute and I believe it is best practice.

3 Comments

yes. But the link[w3schools.com/jsref/jsref_Number.asp] shows that passing a String value to Number object gives a NaN
But you did not say Number("XXX ..."). You said "XXX ...".
You said "XXX ...." and not Number("XXX ..."). The latter will give you an error because the "constructor" expects a number.
1

It won't raise an error because when you do:

numberReprsen = "XXX again";

You are actually changing its type to string and overwriting its previous value.

1 Comment

Technically, you are not changing the type of anything. You are discarding one value and replacing it with another. The variable has no type to begin with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.