JavaScript syntax is the set of rules that define how to write JavaScript code.
1. *Variables*:
- Declared using `let`, `const`, or `var`- Assigned using the assignment operator (`=`)
- Example: `let x = 5;`
2. *Data Types*:
- Numbers: `42`- Strings: `"hello"`
- Booleans: `true` or `false`
- Arrays: `[1, 2, 3]`
- Objects: `{ name: "John", age: 30 }`
3. *Operators*:
- Arithmetic: `+`, `-`, `*`, `/`, `%`- Comparison: `==`, `!=`, `===`, `!==`, `>`, `<`, `>=` , `<=`
- Logical: `&&`, `||`, `!`
- Assignment: `=`, `+=`, `-=`, `*=`, `/=`, `%=`
4. *Control Structures*:
- Conditional statements: `if`, `else`, `switch`- Loops: `for`, `while`, `do...while`
- Functions: `function`, `return`
5. *Functions*:
- Declared using the `function` keyword- Can take arguments and return values
- Example: `function add(x, y) { return x + y; }`
6. *Syntax*:
- Curly braces `{}` used to define code blocks- Parentheses `()` used to group expressions
- Semicolons `;` used to end statements
- Commas `,` used to separate items in arrays and objects
Here's an example of JavaScript syntax in action:
```
let name = "John";
let age = 30;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
function greet(name) {
console.log("Hello, " + name + "!");
}
greet(name); // Output: Hello, John!
```
This code demonstrates variables, data types, operators, control structures, and functions, all of which are essential elements of JavaScript syntax.
0 टिप्पणियाँ