JavaScript Syntax
- JavaScript syntax defines the rules for writing JavaScript code
- It tells the browser how JavaScript statements should be written
- JavaScript syntax includes variables, values, operators, functions, and statements
- Correct syntax helps JavaScript programs run without errors
What is JavaScript Syntax?
JavaScript syntax is the set of rules that defines how JavaScript programs are written. Just like every language has grammar rules, JavaScript also has specific rules that developers must follow to write correct code.
JavaScript code is made up of statements. Each statement performs a specific task, such as creating variables, displaying output, or executing functions.
JavaScript Statements
A JavaScript statement is a single instruction that tells the browser to perform an action. Statements are usually separated by a semicolon (;).
let message = "Hello JavaScript";
console.log(message);
JavaScript Variables
Variables are used to store data values. JavaScript provides three keywords to declare variables: var, let, and const.
let name = "Shivam";
const age = 21;
console.log(name);
console.log(age);
JavaScript is Case Sensitive
JavaScript is a case-sensitive language. This means uppercase and lowercase letters are treated as different characters.
let name = "Alex";
console.log(name);
console.log(Name);
name and Name are different because JavaScript treats uppercase and lowercase letters differently.
JavaScript Comments
Comments are used to explain JavaScript code. They are ignored by the browser and do not affect program execution.
// This is a single line comment
let value = 10;
JavaScript Functions
Functions are blocks of reusable code designed to perform a specific task.
function greet() {
console.log("Hello World");
greet();
JavaScript Code Blocks
JavaScript code blocks are written inside curly braces { }.
They group multiple statements together.
if (age >= 18 ) {
console.log("Adult");
}
Important JavaScript Syntax Rules
Used to separate JavaScript statements.
Used to define code blocks.
Used to create string values.
Uppercase and lowercase are different.