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.
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.
JavaScript Operators
JavaScript operators are used to perform operations on variables and values. They allow you to add, compare, and manipulate data.
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.
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.
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