JavaScript Syntax
JavaScript syntax defines the set of rules that determine how JavaScript programs are written and interpreted. Understanding syntax is essential for writing correct and error-free code.
JavaScript Statements
A JavaScript program is made up of statements. These statements are executed by the browser one by one in the order they are written.
<!DOCTYPE html> <html> <body> same w3schools method use next time method change <p id="demo"></p> <script> let x = 10; let y = 5; let z = x + y; document.getElementById("demo").innerHTML = "The value of z is" + z; </script> </body> </html>Try this code
Each line above is a JavaScript statement that performs a specific action.
JavaScript Variables
Variables are used to store data values. In JavaScript, variables can be declared using
var, let, or const.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> let name = "John"; document.getElementById("demo").innerHTML = name; </script> </body> </html>Try this code
JavaScript Operators
JavaScript operators are used to perform operations on variables and values. They allow you to add, compare, and manipulate data.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> let x = 10; let y = 5; document.getElementById("demo").innerHTML = x + y; </script> </body> </html>Try this code
JavaScript Expressions
An expression is a combination of values, variables, and operators that produces a result.
5 + 10 x * 2
JavaScript Keywords
JavaScript keywords are reserved words that have special meaning in the language. They cannot be used as variable names.
- let
- const
- var
- if
- else
- function
- return
JavaScript Comments
Comments are used to explain code and make it more readable. They are ignored by the browser.
// This is a single-line comment /* This is a multi-line comment */
JavaScript is Case Sensitive
JavaScript is case sensitive. This means that variables and functions must be written with the correct capitalization.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> let name = "Ali"; let Name = "Ahmed"; document.getElementById("demo").innerHTML = Name; </script> </body> </html>Try this code
In the example above, name and Name are treated as two different variables.
Semicolons
Semicolons are used to separate JavaScript statements. Although they are optional in many cases, it is recommended to use them for better code readability and consistency.
let a = 5; let b = 10;
Code Blocks
JavaScript code blocks are written inside curly braces { }.
They are used to group multiple statements together.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <button onclick="greet()">Click Me</button> <script> function greet() { let message = "Hello"; document.getElementById("demo").innerHTML = message; } </script> </body> </html>Try this code
Whitespace in JavaScript
JavaScript ignores extra spaces and line breaks. You can format your code with whitespace to make it more readable.
let x=5; let y = 5;
Best Practices
- Use meaningful variable names
- Always use semicolons
- Keep code clean and readable
- Use proper indentation