Simple Code not working ... Please help

Write a JavaScript Program to Check Whether a Given Value Type is Number or Not

<script>

function number_checker (value) {
	if (typeof value == number) {
		document.write ("This is a Number");
	}
	else {
		document.write ("This is Not a Number");
	}	
}

number_checker (3);

</script>

Its giving no output. what am I doing wrong?

Do you know how to use the browser console? It’s ctrl+shift+j, or F12 when using internet explorer.

It will tell you that the variable called number has not been defined.

Ohh thank you. Yes I was having doubt regarding that point.

Changing the code to === “number” solved the issue.

Thank you :slight_smile:

you still have the issue of using antiquated document.write statements that don’t work properly when you put the JavaScript at the bottom of the page where it belongs.

Can you explain a bit more please…

put all JavaScript just before the </body> tag - it has no place anywhere else (apart from a very small number of small scripts that have to go in the head because they need to run before the page starts to load.

never ever use document.wrute - it never works properly when you put your JavaScript where the JavaScript should go - it is as obsolete as Netscape 4 which was the last browser that needed it).

Ohh OK… Thaank you.

But the problem is return statement don’t works in my browser. I don’t know why.

Here is my full code:

<!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">
<title>1</title>
</head>
<body>

<script>

function number_checker (value) {
	if (typeof value === "number") {
		return "This is a Number";
	}
	else {
		return "This is Not a Number";
	}	
}

number_checker (3);

</script>

</body>
</html>

If you change the code like this, what do you see in the console

...
	else {
		return "This is Not a Number";
	}	
}
console.log(number_checker(3));
</script>
</body>

Its working in the console but I want to see in normal browser. When I click on the index.html file, I want to see the output.

How can I do that. Till now I was using document.write() but Felgall told me not to use it.

Then the function is returning OK.

The problem is changing from having the script where you want the output to appear to having the script at the bottom and putting the output where you want it to appear.

This can be done like

<div id="function_result"></div>
var result_holder = document.getElementById("function_result");
result_holder.textContent = number_checker(3);
1 Like

Thank you Mitti.

Problem solved :slight_smile:

Thank you to all of you :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.