Sharpen your JavaScript skills by identifying and correcting common coding errors with these beginner-friendly practice exercises.
Identify and fix the mistake in the following code:
<script>
function greet() {
alert("Welcome to JavaScript!"
}
greet();
</script>
Hint: ๐ก Check the syntax of the function, especially parentheses and braces.
Issue: The closing parenthesis and semicolon are missing from the alert statement.
<script>
function greet() {
alert("Welcome to JavaScript!");
}
greet();
</script>
Identify and fix the mistake in the following code:
<script>
var name = prompt("What is your name?")
alert("Hello, " + name)
</script>
Hint: ๐ก Always terminate statements with a semicolon in JavaScript.
Issue: Missing semicolons at the end of statements.
<script>
var name = prompt("What is your name?");
alert("Hello, " + name);
</script>
Identify and fix the mistake in the following code:
<script>
alert(Welcome to JavaScript);
</script>
Hint: ๐ก Text inside alert should be in quotes.
Issue: The string is not wrapped in quotation marks.
<script>
alert("Welcome to JavaScript");
</script>
Identify and fix the mistake in the following code:
<script>
document.getElementById("message").innerHtml = "Hello World!";
</script>
Hint: ๐ก JavaScript property names are case-sensitive.
Issue: innerHtml is incorrect; it should be <b>innerHTML</b>.
<script>
document.getElementById("message").innerHTML = "Hello World!";
</script>
Identify and fix the mistake in the following code:
<script src="script.js">
alert("This won't work!");
</script>
Hint: ๐ก Avoid mixing inline script content when using the `src` attribute.
Issue: When using the `src` attribute in the script tag, inline code inside it is ignored.
<script src="script.js"></script>
<!-- Or remove src and include the code inline -->
<script>
alert("This won't work!");
</script>