π What Youβll Learn
- What JavaScript syntax is
- The basic rules of writing JavaScript
- Common elements like variables, values, operators, and keywords
- Syntax structure examples
π What Is Syntax?
Syntax refers to the set of rules that define how JavaScript code is written and structured. Just like grammar in a language, syntax ensures your code is valid and understandable by the browser.
π€ JavaScript Is Case-Sensitive
JavaScript treats uppercase and lowercase letters differently.
let name = "Alex";
let Name = "Taylor";
console.log(name); // Alex
console.log(Name); // Taylor
These are two different variables!
π§± JavaScript Structure
A basic JavaScript program is made up of:
- Statements β actions or instructions (
let x = 5;) - Blocks β code grouped inside
{} - Semicolons β used to end statements (optional but recommended)
π· Identifiers and Keywords
- Identifiers are names you assign (e.g.,
userName,total) - Keywords are reserved words in JavaScript (e.g.,
let,const,if,function)
let total = 100;
π’ Values and Variables
JavaScript uses variables to store values.
let message = "Hello!";
let age = 25;
Values can be numbers, text (strings), booleans, arrays, etc.
β Operators
JavaScript supports various operators to perform operations:
- Arithmetic:
+,-,*,/ - Assignment:
=,+=,-= - Comparison:
==,===,!=,> - Logical:
&&,||,!
Example:
let result = 10 + 5;
π Comments
You can add notes in your code using comments:
// This is a single-line comment
/*
This is a multi-line comment
*/
Comments are ignored by the browser and help others understand your code.
π Recap
- JavaScript syntax defines how code is written
- Case sensitivity and semicolons matter
- Use variables, operators, and keywords correctly
- Always comment your code for clarity