Syntax 2
JavaScript is a scripting language that is widely used in web development. Here are some basic syntax rules of JavaScript:
- Statements: In JavaScript, a statement is a line of code that performs an action. Statements end with a semicolon (;) by convention. For example:
javascriptvar x = 5;
alert("Hello, World!");
- Variables: Variables are used to store values in JavaScript. You can declare a variable using the
var
keyword followed by the variable name. For example:
javascriptvar x = 5;
var y = "Hello";
- Data types: JavaScript has several data types, including numbers, strings, booleans, arrays, and objects. For example:
javascriptvar x = 5; // number
var y = "Hello"; // string
var z = true; // boolean
var arr = [1, 2, 3]; // array
var obj = {name: "John", age: 30}; // object
- Operators: JavaScript has several types of operators, including arithmetic, assignment, comparison, and logical operators. For example:
javascriptvar x = 5;
var y = 10;
var z = x + y; // addition
x += 1; // assignment
if (x == 5 && y > 8) { // logical operators
// do something
}
- Functions: Functions are reusable blocks of code that perform a specific task. You can define a function using the
function
keyword followed by the function name and parameters. For example:
scssfunction greet(name) {
alert("Hello, " + name + "!");
}
greet("John");
These are some of the basic syntax rules of JavaScript, but there are many more advanced features and concepts to explore
- Conditional statements: JavaScript has if, else if, and else statements for conditional execution of code. For example:
javascriptvar age = 18;
if (age < 18) {
console.log("You are not old enough to vote.");
} else if (age >= 18 && age <= 65) {
console.log("You are eligible to vote.");
} else {
console.log("You are past the age of voting.");
}
- Loops: JavaScript has several types of loops, including
for
loops,while
loops, anddo-while
loops. Loops are used to repeatedly execute a block of code. For example: - Arrays: Arrays are used to store multiple values in a single variable. You can create an array using square brackets and separate the values with commas. For example:
cssfor (var i = 0; i < 5; i++) {
console.log(i);
}
var i = 0;
while (i < 5) {
console.log(i);
i++;
}
javascriptvar fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // prints "apple"
- Objects: Objects are used to store collections of properties and values. You can create an object using curly braces and separating the properties with commas. For example:
javascriptvar person = {
name: "John",
age: 30,
address: {
street: "123 Main St",
city: "Anytown",
state: "CA"
}
};
console.log(person.name); // prints "John"
console.log(person.address.city); // prints "Anytown"
- Functions: Functions can take parameters and return values. You can call a function using its name followed by parentheses. For example:
javascriptfunction addNumbers(x, y) {
return x + y;
}
var sum = addNumbers(5, 10);
console.log(sum); // prints 15
These are just a few more examples of the syntax concepts in JavaScript. JavaScript is a very versatile language with many features and capabilities.
Comments
Post a Comment