syntax
- Dapatkan link
- X
- Aplikasi Lainnya
syntak javascript
Of course! "syntak javascript" likely refers to the syntax of JavaScript.
JavaScript syntax is the set of rules that define how to write a correctly structured JavaScript program. It's the grammar of the language.
Here is a comprehensive breakdown of the fundamental syntax rules in JavaScript.
1. Basic Structure
Statements
A computer program is a list of "instructions" to be executed. These instructions are called statements.
let x = 5; // A statement let y = 10; // Another statement console.log(x + y); // Another statement
Semicolons (;)
Semicolons separate statements. They are often optional due to Automatic Semicolon Insertion (ASI), but it's considered good practice to use them to avoid unexpected errors.
// Good practice let a = 1; let b = 2; // Can work, but riskier let a = 1 let b = 2
Whitespace and Line Breaks
JavaScript ignores multiple spaces, tabs, and newlines. You can use them to make your code more readable.
// These are the same let user="John"; let user = "John"; // Easier to read
2. Comments
Comments are ignored by the JavaScript engine and are used to explain your code.
// This is a single-line comment /* This is a multi-line comment */
3. Case Sensitivity
JavaScript is case-sensitive. A variable named myVar is different from myvar or MYVAR.
let myVar = "Hello"; let myvar = "World"; console.log(myVar); // "Hello" console.log(myvar); // "World"
4. Values (Literals & Variables)
Literals (Fixed Values)
Numbers:
10,10.5,-5Strings:
"Hello",'World',`Template Literal`Booleans:
true,falseArrays:
[1, 2, 3, 4]Objects:
{name: "John", age: 30}null and undefined
Variables (Containers for Values)
You declare variables using const, let, or the older var.
let age = 25; // Can be reassigned const pi = 3.14159; // Cannot be reassigned var oldName = "John"; // Older way, function-scoped age = 26; // This is fine // pi = 3; // This will cause an error!
5. Identifiers (Naming Rules)
Identifiers are names for variables, functions, etc.
Can contain letters, digits, underscores (
_), and dollar signs ($).Must begin with a letter,
$, or_.Reserved words (like
let,if,for) cannot be used.
Good: myVar, _temp, $element, user123
Bad: 1stPlace, my-var, let
6. Operators
Arithmetic Operators
+, -, *, /, % (remainder), ** (exponentiation)
let sum = 10 + 5; // 15 let power = 2 ** 3; // 8 (2*2*2)
Assignment Operators
=, +=, -=, *=, /=
let x = 10; x += 5; // x is now 15 (same as x = x + 5)
Comparison Operators
== (equal to), === (strict equal to), !=, !==, >, <, >=, <=
console.log(5 == "5"); // true (compares value) console.log(5 === "5"); // false (compares value AND type)
Logical Operators
&& (and), || (or), ! (not)
if (age > 18 && hasLicense) { console.log("Can drive"); }
7. Data Types
JavaScript has dynamic types; a variable can hold different data types.
// typeof operator tells you the type let message = "Hello"; // string let score = 100; // number let isActive = true; // boolean let data = null; // null (object) let notDefined; // undefined let person = { firstName: "Jane" }; // object let list = [1, 2, 3]; // object (array is a special kind of object)
8. Functions
A block of code designed to perform a particular task.
// Function Declaration function greet(name) { return "Hello, " + name; } // Function Expression (assigned to a variable) const square = function(x) { return x * x; }; // Arrow Function (ES6+) const add = (a, b) => a + b; // Calling a function let result = greet("Alice"); // "Hello, Alice"
9. Control Flow
Conditional Statements
// if...else let time = 10; if (time < 12) { console.log("Good morning"); } else { console.log("Good afternoon"); } // switch switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; default: day = "Another day"; }
Loops
// for loop for (let i = 0; i < 5; i++) { console.log(i); } // while loop let i = 0; while (i < 5) { console.log(i); i++; } // for...of loop (for arrays, strings) const arr = ['a', 'b', 'c']; for (const element of arr) { console.log(element); }
10. Objects and Arrays
Objects (key-value pairs)
const car = { type: "Fiat", model: "500", color: "white", start: function() { // Method (a function inside an object) console.log("Car started"); } }; // Accessing properties console.log(car.model); // Dot notation console.log(car["color"]); // Bracket notation car.start(); // Calling a method
Arrays (ordered lists)
const fruits = ["Apple", "Banana", "Mango"]; // Accessing by index (starts at 0) console.log(fruits[0]); // "Apple" // Common array methods fruits.push("Orange"); // Adds to the end fruits.pop(); // Removes from the end
11. Code Blocks ({})
Curly braces {} are used to define blocks of code, such as in functions, loops, and conditionals.
if (true) { // This is a code block let blockScopedVar = "I exist only here"; console.log(blockScopedVar); } // console.log(blockScopedVar); // Error! blockScopedVar is not defined here
This covers the core syntax of JavaScript. Mastering these fundamentals is the first and most crucial step to becoming proficient in the language.
- Dapatkan link
- X
- Aplikasi Lainnya
Komentar
Posting Komentar