Hướng dẫn dùng poperator JavaScript
JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more. Show Nội dung chính
JavaScript Arithmetic OperatorsArithmetic operators are used to perform arithmetic between variables and/or values. Given that y = 5, the table below explains the arithmetic operators:
For a tutorial about arithmetic operators, read our JavaScript Arithmetic Tutorial. JavaScript Assignment OperatorsAssignment operators are used to assign values to JavaScript variables. Given that x = 10 and y = 5, the table below explains the assignment operators:
For a tutorial about assignment operators, read our JavaScript Assignment Tutorial. JavaScript String OperatorsThe + operator, and the += operator can also be used to concatenate (add) strings. Given that text1 = "Good ", text2 = "Morning", and text3 = "", the table below explains the operators:
Comparison OperatorsComparison operators are used in logical statements to determine equality or difference between variables or values. Given that x = 5, the table below explains the comparison operators:
For a tutorial about comparison operators, read our JavaScript Comparisons Tutorial. Conditional (Ternary) OperatorThe conditional operator assigns a value to a variable based on a condition.
Example explained: If the variable "age" is a value below 18, the value of the variable "voteable" will be "Too young", otherwise the value of voteable will be "Old enough". Logical OperatorsLogical operators are used to determine the logic between variables or values. Given that x = 6 and y = 3, the table below explains the logical operators:
JavaScript Bitwise OperatorsBit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers. Because of this, in JavaScript, ~ 5 will not return 10. It will return -6. ~00000000000000000000000000000101 will return 11111111111111111111111111111010 The typeof OperatorThe typeof operator returns the type of a variable, object, function or expression: Example typeof "John"
// Returns string Try it Yourself » Please observe:
You cannot use typeof to define if a JavaScript object is an array (or a date). The delete OperatorThe delete operator deletes a property from an object: Example const person = { Try it Yourself » The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions. Note: The delete operator should not be used on predefined JavaScript object properties. It can crash your application. The in OperatorThe in operator returns true if the specified property is in the specified object, otherwise false: Example // Arrays // Objects // Predefined objects Try it Yourself » The instanceof OperatorThe instanceof operator returns true if the specified object is an instance of the specified object: Exampleconst cars = ["Saab", "Volvo", "BMW"]; (cars instanceof Array) // Returns true Try it Yourself » The void OperatorThe void operator evaluates an expression and returns undefined. This operator is often used to obtain the undefined primitive value, using "void(0)" (useful when evaluating an expression without using the return value). Example Try it Yourself » |