Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - JavaScript Syntax


JavaScript Syntax

JavaScript syntax is the set of rules that tells the browser how to read and run your code. It's like grammar for your web page's language.


JavaScript Statements

JavaScript programs are made up of statements. Each statement is a command that the browser runs, one after another.

Example

JavaScript changes the text inside an HTML element:

<p id="demo">Hello</p>

<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";  // This is a statement
</script>
Try it Yourself »

Each line is a JavaScript statement that tells the browser what to do.


Semicolons ;

You can end JavaScript statements with a semicolon (;). It tells the browser that the command is complete.

Example

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "First line";
document.getElementById("demo").style.color = "blue";
</script>
Try it Yourself »

Note: Semicolons are optional in many cases, but using them makes your code easier to read and prevents small errors.


White Space and Line Breaks

JavaScript ignores extra spaces and line breaks. You can use them to make your code more readable.

Example

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML="Hello";
document.getElementById( "demo" ).innerHTML = "Hello";  // Preferred
document . getElementById ( "demo" ) . innerHTML = "Hello";
</script>

All three lines above do the same thing - spaces don't matter.


Case Sensitivity

JavaScript is case sensitive. That means document.getElementById and Document.getelementbyid are not the same.

Example

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "This works!";
Document.getElementById("demo").innerHTML = "This will cause an error";
</script>
Try it Yourself »

JavaScript Code Blocks

Code blocks group statements together using curly braces { }. You'll see them more when learning about functions and loops.

Example

<p id="demo"></p>

<script>
{
  document.getElementById("demo").innerHTML = "Hello!";
  document.getElementById("demo").style.color = "green";
}
</script>

Summary

  • JavaScript code is made of statements that tell the browser what to do.
  • Semicolons (;) end statements.
  • Extra spaces and line breaks are ignored.
  • JavaScript is case sensitive.
  • Code blocks group multiple statements together.

Next, you'll learn how to store and reuse information in JavaScript.

Next » JavaScript Variables


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.