GedankenNebel
GedankenNebel

Reputation: 2587

Can not understand behaviour by passing arguments to Javascript functions

Consider following HTML Snippet with Javascript in it. Paste this code here to see that stuff running in the Tryit Editor from W3Schools.

<!DOCTYPE html>
<html>
<body>
<a href="javascript: submitQuickAddForm('00001030')">Number with quotes</a><br>
<a href="javascript: submitQuickAddForm(00001030)">Number without quotes</a> <br>
<a href="javascript: submitQuickAddForm(hello)">Text with quotes</a> <br>
<a href="javascript: submitQuickAddForm('hello')">Text without quotes</a>
<script>
function submitQuickAddForm(itemNumber) {
alert(itemNumber);
}
</script>

</body>
</html>

The first link puts out 00001030. The second: 536 Which I cant understand at all ?! The third link does nothing (maybe no type for the parameter specified) and the last link works well again (output: hello)

So whats with the second and third link?

Upvotes: 0

Views: 44

Answers (1)

Anto Jurković
Anto Jurković

Reputation: 11258

The first one is string, the second one is interpreted as octal number because of leading 0.

EDIT: Explanation from @RobG to question: "but why can´t it alert just 00001030 or at least 1030? Why 536?"

Because Number.prototype.toString assumes a radix of 10

Update: The 3rd link does nothing because hello is not defined. Most probably you got error in console: ReferenceError: hello is not defined

Upvotes: 4

Related Questions