📅  最后修改于: 2023-12-03 15:31:39.495000             🧑  作者: Mango
JavaScript is a popular programming language used primarily for web applications. It is a dynamically typed language, meaning the type of a variable is determined at runtime.
In JavaScript, variables are declared using the var
, let
, or const
keyword, followed by the variable name. The var
keyword declares a variable globally or locally to the entire function. The let
keyword declares a block-scoped variable, while the const
keyword declares a block-scoped constant value that cannot be reassigned.
Here is an example:
var x = 5;
let y = 10;
const z = 15;
JavaScript supports several built-in data types, including numbers, strings, booleans, null, undefined, and objects.
let num = 5; // number
let str = "Hello, world!"; // string
let bool = true; // boolean
let n = null; // null
let u; // undefined
let obj = {name: "John", age: 30}; // object
JavaScript supports different types of operators, including arithmetic, comparison, logical, assignment, and bitwise operators.
let x = 5, y = 10;
let sum = x + y; // addition
let diff = x - y; // subtraction
let prod = x * y; // multiplication
let quot = x / y; // division
let mod = x % y; // modulus
let isEqual = x == y; // equality check
let isNotEqual = x != y; // inequality check
let isLess = x < y; // less than
let isGreater = x > y; // greater than
let isTrue = true, isFalse = false;
let and = isTrue && isFalse; // logical AND
let or = isTrue || isFalse; // logical OR
let not = !isTrue; // logical NOT
let a = 5;
a += 3; // a = a + 3;
a -= 1; // a = a - 1;
a *= 2; // a = a * 2;
a /= 4; // a = a / 4;
a %= 2; // a = a % 2;
let num = 5; // 0101
let bitwiseAnd = num & 1; // 0001
let bitwiseOr = num | 2; // 0110
let bitwiseNot = ~num; // 1010
JavaScript supports different types of control flow statements, including if/else statements, switch statements, while loops, do/while loops, and for loops.
let x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}
let day = "Monday";
switch (day) {
case "Monday":
console.log("Today is Monday");
break;
case "Tuesday":
console.log("Today is Tuesday");
break;
case "Wednesday":
console.log("Today is Wednesday");
break;
default:
console.log("Today is not a weekday");
break;
}
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
let j = 5;
do {
console.log(j);
j--;
} while (j > 0);
for (let i = 0; i < 5; i++) {
console.log(i);
}
JavaScript functions are declared using the function
keyword, followed by the function name, and a list of parameters enclosed in parentheses. The function body is enclosed in curly brackets.
function add(x, y) {
return x + y;
}
let s = add(5, 10); // s = 15
In JavaScript, objects are collections of key-value pairs, where the keys are strings and the values can be any data type.
let person = {
name: "John",
age: 30,
isMarried: false,
hobbies: ["music", "reading", "sports"],
address: {
street: "Main St",
city: "New York",
zip: "10001"
}
};
console.log(person.name); // "John"
console.log(person.hobbies[1]); // "reading"
console.log(person.address.city); // "New York"
This is just a brief overview of JavaScript syntax. There is a lot more to learn, but this should give you a good starting point. Happy coding!