JavaScript Operator referenshandledning
- Föregående sida JS objekt
- Nästa sida JS operatorernas prioriteringsordning
JavaScript-operatorer används för tilldelning, jämförelse av värden, utförande av aritmetiska operationer med mera.
Se också:
Lär dig mer:JavaScript operatorer
Lär dig mer:JavaScript Operator prioritet
JavaScript aritmetiska operatorer
Aritmetiska operatorer används för att utföra aritmetiska operationer mellan variabler och/eller värden.
Given y = 5,nedanstående tabell förklarar aritmetiska operatorer:
Operator | Description | Example | resultatet i y | resultatet i x | Try it |
---|---|---|---|---|---|
+ | addition | x = y + 2 | y = 5 | x = 7 | Try it |
- | subtraktion | x = y - 2 | y = 5 | x = 3 | Try it |
* | multiplication | x = y * 2 | y = 5 | x = 10 | Try it |
/ | division | x = y / 2 | y = 5 | x = 2.5 | Try it |
% | modulus (delningsrest) | x = y % 2 | y = 5 | x = 1 | Try it |
++ | inkrement | x = ++y | y = 6 | x = 6 | Try it |
x = y++ | y = 6 | x = 5 | Try it | ||
-- | minskning | x = --y | y = 4 | x = 4 | Try it |
x = y-- | y = 4 | x = 5 | Try it |
För kunskap om aritmetiska operatorer, läs vår JavaScript-arithmetisk handledning.
JavaScript-tillsalingsoperatorer
Tillsalingsoperatorer används för att tilldela värden till JavaScript-variabler.
Given x = 10 and y = 5,nedanstående tabell förklarar tillsalingsoperatorn:
Operator | Example | Is equivalent to | resultatet i x | Try it |
---|---|---|---|---|
= | x = y | x = y | x = 5 | Try it |
+= | x += y | x = x + y | x = 15 | Try it |
-= | x -= y | x = x - y | x = 5 | Try it |
*= | x *= y | x = x * y | x = 50 | Try it |
/= | x /= y | x = x / y | x = 2 | Try it |
%= | x %= y | x = x % y | x = 0 | Try it |
För kunskap om tillsalingsoperatorer, läs vår JavaScript-tilsalingshandledning.
JavaScript-strängoperatorer
+-operatorn och +=-operatorn kan också användas för att koppla (lägga till) strängar.
Given text1 = "God "、text2 = "Morning",och text3 = "",nedanstående tabell förklarar denna operator:
Operator | Example | text1 | text2 | text3 | Try it |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "God " | "Morning" | "God morgon" | Try it |
+= | text1 += text2 | "God morgon" | "Morning" | "" | Try it |
jämförelseoperatorer
Använd jämförelseoperatorn i logiska uttryck för att avgöra likhet eller skillnad mellan variabler eller värden.
Given x = 5,nedanstående tabell förklarar jämförelseoperatorn:
Operator | Description | jämföra | returnera | Try it |
---|---|---|---|---|
== | likar | x == 8 | false | Try it |
x == 5 | true | Try it | ||
=== | likavärden och likatyper | x === "5" | false | Try it |
x === 5 | true | Try it | ||
!= | inte lika | x != 8 | true | Try it |
!== | Unequal values or unequal types | x !== "5" | true | Try it |
x !== 5 | false | Try it | ||
> | Greater than | x > 8 | false | Try it |
< | Less than | x < 8 | true | Try it |
>= | Greater than or equal to | x >= 8 | false | Try it |
<= | Less than or equal to | x <= 8 | true | Try it |
For knowledge about comparison operators, please read our JavaScript comparison tutorial.
Conditional (ternary) operator
The conditional operator assigns a value to a variable based on the condition.
Syntax
variablename = (condition) ? value1:value2
Exempel
voteable = (age < 18) ? "Too young":"Old enough";
Example explanation:
If the value of the variable "age" is less than 18, the value of the variable "voteable" will be "Too young", otherwise, the value of "voteable" will be "Old enough".
Logical operators
Logical operators are used to determine the logic between variables or values.
Given x = 6 and y = 3The following table explains the logical operators:
Operator | Description | Example | Try it |
---|---|---|---|
&& | and | (x < 10 && y > 1) is true | Try it |
|| | or | (x === 5 || y === 5) is false | Try it |
! | not | !(x === y) is true | Try it |
JavaScript bitwise operators
Bitwise operators can handle 32-bit numbers. Any numeric operand in this operation will be converted to a 32-bit number. The result will be converted back to a JavaScript number.
Operator | Description | Example | Is equivalent to | Result | Decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | x = ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | x = 5 ^ 1 | 0101 ^ | 0100 | 4 |
<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
Exemplet ovan använder ett 4-bitars utan tecken. Men JavaScript använder 32-bitars tecken
Därför returnerar ~ 5 inte 10 i JavaScript, utan -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010
typeof operator
typeof The operator returns the type of a variable, object, function, or expression:
Exempel
typeof "Bill" // returns string typeof 3.14 // returns number typeof NaN // returns number typeof false // returns boolean typeof [1, 2, 3, 4] // returns object typeof {name:'Bill', age:19} // returns object typeof new Date() // returns object typeof function () {} // returns function typeof myCar // returns undefined (if myCar is not declared) typeof null // returns object
Note that:
- The data type of NaN is number
- The data type of an array is object
- The data type of a date is object
- The data type of null is object
- The data type of an undefined variable is undefined
You cannot use typeof to define whether a JavaScript object is an array (or a date).
You cannot use typeof to define whether a JavaScript object is an array (or a date).
delete operator
delete The operator deletes properties from the object:
Exempel
var person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"}; delete person.age; // delete person["age"];
The delete operator deletes both the value of the property and the property itself.
Before re-adding it after deletion, the property cannot be used.
delete operator is intended to be used for object properties. It has no effect on variables or functions.
Kommentar:Det bör inte användas delete operatorn på fördefinierade JavaScript-objektsegenskaper. Detta kan leda till att ditt program kraschar.
in operatorn
om den specifika egenskapen finns i det specifika objektet: in operatorn returnerar true, annars returnerar den false:
Exempel
// Array var cars = ["Saab", "Volvo", "BMW"]; "Saab" in cars // Returnerar false (angivna indexnummer istället för värden) 0 in cars // Returnerar true 1 in cars // Returnerar true 4 in cars // Returnerar false (finns inte) "length" in cars // Returnerar true (length är en arrayegenskap) // Objekt var person = {firstName:"Bill", lastName:"Gates", age:19}; "firstName" in person // Returnerar true "age" in person // Returnerar true // Fördefinierade objekt "PI" in Math // Returnerar true "NaN" in Number // Returnerar true "length" in String // Returnerar true
instanceof operatorn
om den specifika objektet är en instans av det specifika objektet: instanceof operatorn returnerar true:
Exempel
var cars = ["Saab", "Volvo", "BMW"]; cars instanceof Array; // Returnerar true cars instanceof Object; // Returnerar true cars instanceof String; // Returnerar false cars instanceof Number; // Returnerar false
void operatorn
void operatorn räknar ut ett uttryck och returnerar undefinedDenna operator används vanligtvis för att få odefinierade ursprungliga värden med "void(0)" (mycket användbart när man räknar ut uttryck utan att använda returvärdet).
Exempel
<a href="javascript:void(0);"> Onödig länk </a> <a href="javascript:void(document.body.style.backgroundColor='red');"> Klicka här för att ändra bakgrundsfärgen på body till röd </a>
- Föregående sida JS objekt
- Nästa sida JS operatorernas prioriteringsordning