1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
|
---
title: Ausdrücke und Operatoren
slug: Web/JavaScript/Guide/Expressions_and_Operators
tags:
- Beginner
- Extensions
- Guide
- JavaScript
- Operatoren
- l10n:priority
translation_of: Web/JavaScript/Guide/Expressions_and_Operators
original_slug: Web/JavaScript/Guide/Ausdruecke_und_Operatoren
---
<div>{{jsSidebar("JavaScript Guide")}} {{PreviousNext("Web/JavaScript/Guide/Funktionen", "Web/JavaScript/Guide/Numbers_and_dates")}}</div>
<p class="summary">Dieses Kapitel beschreibt JavaScript Ausdrücke und Operatoren, Zuweisungsoperatoren, Vergleichsoperatoren, Rechenoperatoren, Bit-Operatoren, Logische Operatoren, Operator zur Zeichenkettenverknüpfung, Bedingte (ternäre) Operatoren und mehr.</p>
<p>Eine vollständige und detaillierte Liste mit Operatoren und Ausdrücken ist in den <a href="/de-DE/docs/Web/JavaScript/Reference/Operators">Referenzen</a> zu finden.</p>
<h2 id="Operatoren">Operatoren</h2>
<p>JavaScript besitzt verschiedene Operatortypen. Dieser Abschnitt beschreibt die einzelnen Operatoren und beinhaltet Informationen über die Operator-Prioritäten.</p>
<ul>
<li>{{ web.link("#Assignment_operators", "Zuweisungs-Operatoren") }}</li>
<li>{{ web.link("#Comparison_operators", "Vergleichs-Operatoren") }}</li>
<li>{{ web.link("#Arithmetic_operators", "Arithmetische Operatoren") }}</li>
<li>{{ web.link("#Bitwise_operators", "Bit-Operatoren") }}</li>
<li>{{ web.link("#Logical_operators", "Logische Operatoren") }}</li>
<li>{{ web.link("#String_operators", "String Operatoren") }}</li>
<li>{{ web.link("#Conditional_(ternary)_operator", "Bedingter (ternärer) Operator")}}</li>
<li>{{ web.link("#Comma_operator", "Komma Operator")}}</li>
<li>{{ web.link("#Unary_operators", "Unäre Operatoren")}}</li>
<li>{{ web.link("#Relational_operators", "Vergleichsoperator")}}</li>
</ul>
<p>JavaScript verfügt über beides, binäre als auch unäre Operatoren. Zudem existiert ein spezieller ternärer Operator - der Bedingungsoperator. Ein binärer Operator benötigt zwei Operanden, einen vor dem Operator und einen nach dem Operator:</p>
<pre class="syntaxbox"><em>operand1</em> <em>operator</em> <em>operand2</em>
</pre>
<p>Zum Beispiel: <code>3+4</code>, oder <code>x*y</code>.</p>
<p>Ein unärer Operator erwartet einen einzelnen Operanden, entweder vor, oder nach dem Operator:</p>
<pre class="syntaxbox"><em>operator</em> <em>operand</em>
</pre>
<p>oder</p>
<pre class="syntaxbox"><em>operand</em> <em>operator</em>
</pre>
<p>Zum Beispiel: <code>x++</code>, oder <code>++x</code>.</p>
<h3 id="Zuweisungsoperatoren">Zuweisungsoperatoren</h3>
<p>Ein Zuweisungsoperator weißt seinem linken Operanden einen Wert zu. Dieser Wert basiert auf dem Ergebnis des rechten Operanden. Der einfachste Zuweisungsoperator ist das "Gleich" (<code>=</code>), welches den Wert des rechten Operanden dem linken Operanden zuweist.</p>
<p>Zum Beispiel: <code>x = y</code> (<code>y</code> wid der Wert von <code>x</code> zugewiesen).</p>
<p>Es gibt auch zusammengesetzte Zuweisungsoperatoren, diese stellen Abkürzungen für die in der folgenden Tabelle aufgelisteten Operationen dar:</p>
<table class="standard-table">
<caption>Zusammengesetzte Zuweisungsoperatoren</caption>
<thead>
<tr>
<th>Name</th>
<th>Abgekürzter Operator</th>
<th>Ausgeschriebener Operator</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Assignment" title="Zuweisung">Zuweisung</a></td>
<td><code>x = y</code></td>
<td><code>x = y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Addition_assignment">Addition</a></td>
<td><code>x += y</code></td>
<td><code>x = x + y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Subtraction_assignment">Subraktion</a></td>
<td><code>x -= y</code></td>
<td><code>x = x - y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Multiplication_assignment">Multiplikation</a></td>
<td><code>x *= y</code></td>
<td><code>x = x * y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Division_assignment">Division</a></td>
<td><code>x /= y</code></td>
<td><code>x = x / y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Remainder_assignment">Modulo (Division mit Rest)</a></td>
<td><code>x %= y</code></td>
<td><code>x = x % y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Exponentiation_assignment">Exponentiation assignment</a> {{experimental_inline}}</td>
<td><code>x **= y</code></td>
<td><code>x = x ** y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Left_shift_assignment">Left shift assignment</a></td>
<td><code>x <<= y</code></td>
<td><code>x = x << y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Right_shift_assignment">Right shift assignment</a></td>
<td><code>x >>= y</code></td>
<td><code>x = x >> y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Unsigned_right_shift_assignment">Unsigned right shift assignment</a></td>
<td><code>x >>>= y</code></td>
<td><code>x = x >>> y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Bitwise_AND_assignment">Bitwise AND assignment</a></td>
<td><code>x &= y</code></td>
<td><code>x = x & y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Bitwise_XOR_assignment">Bitwise XOR assignment</a></td>
<td><code>x ^= y</code></td>
<td><code>x = x ^ y</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Bitwise_OR_assignment">Bitwise OR assignment</a></td>
<td><code>x |= y</code></td>
<td><code>x = x | y</code></td>
</tr>
</tbody>
</table>
<h4 id="Destrukturierung">Destrukturierung</h4>
<p>Komplexere Zuweisungen ermöglicht Javascript über die sogenannte <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">Destrukturierung</a>. Diese ermöglicht es, Daten aus Arrays oder Objekten mithilfe einer Syntax zu extrahieren, die die Konstruktion von Array- und Objektliteralen widerspiegelt.</p>
<pre class="brush: js">var foo = ["eins", "zwei", "drei"];
// Ohne Destrukturierung
var eins = foo[0];
var zwei = foo[1];
var drei = foo[2];
// mit Destrukturierung
var [eins, zwei, drei] = foo;</pre>
<h3 id="Vergleichsoperatoren">Vergleichsoperatoren</h3>
<p>Ein Vergleichsoperator vergleicht seine Operanden und gibt einen logischen Wert zurück, der darauf basiert, ob der Vergleich wahr ist, oder nicht.</p>
<p>Die Operanden können numerische-, string-, logische- oder Objektwerte sein. Zeichenfolgen werden basierend auf der lexikographischen Standardreihenfolge mit unicodewerten verglichen. Wenn die beiden Operanden nicht vom selben Typ sind, versucht JavaScript in den meisten Fällen, sie in einen geeigneten Typ für den Vergleich zu konvertieren. Dieses Verhalten führt im Allgemeinen dazu, dass die Operanden numerisch verglichen werden. Die einzigen Ausnahmen für die Typumwandlung innerhalb von Vergleichen sind die Operatoren <code>===</code> und <code>!==</code>, die strenge Vergleiche durchführen. Diese Operatoren versuchen nicht, die Operanden in kompatible Typen zu konvertieren, bevor sie die Gleichheit überprüfen.</p>
<p>Die folgende Tabelle beschreibt die Vergleichsoperatoren in Bezug auf diesen Beispielcode:</p>
<pre class="brush: js">var var1 = 3;
var var2 = 4;
</pre>
<table class="standard-table">
<caption>Vergleichsoperatoren</caption>
<thead>
<tr>
<th scope="col">Operator</th>
<th scope="col">Beschreibung</th>
<th scope="col">Beispiele, die <code>true</code> zurückgeben</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Equality">Equal</a> (<code>==</code>)</td>
<td>Gibt <code>true</code> zurück, wenn die Operanden gleich sind.</td>
<td><code>3 == var1</code>
<p><code>"3" == var1</code></p>
<code>3 == '3'</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Inequality">Not equal</a> (<code>!=</code>)</td>
<td>Gibt <code>true</code> zurück, wenn die Operanden ungleich sind.</td>
<td><code>var1 != 4<br>
var2 != "3"</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity">Strict equal</a> (<code>===</code>)</td>
<td>Gibt <code>true</code> zurück, wenn die Operanden gleich sind und auch der Typ übereinstimmt. Weitere Informationen unter {{jsxref("Object.is")}} und <a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness" title="/en-US/docs/Web/JavaScript/Guide/Sameness">sameness in JS</a>.</td>
<td><code>3 === var1</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Nonidentity">Strict not equal</a> (<code>!==</code>)</td>
<td>Gibt <code>true</code> zurück, wenn die Operanden vom selben Typ sind, doch nicht den selben Wert haben, oder wenn sie den selben Wert haben, doch nicht vom selben Typ sind.</td>
<td><code>var1 !== "3"<br>
3 !== '3'</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Greater_than_operator">Greater than</a> (<code>></code>)</td>
<td>Gibt <code>true</code> zurück, wenn der linke Operand größer dem rechten Operanden ist.</td>
<td><code>var2 > var1<br>
"12" > 2</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Greater_than_or_equal_operator">Greater than or equal</a> (<code>>=</code>)</td>
<td>Gibt <code>true</code> zurück, wenn der linke Operand größer als, oder gleich dem linken Operanden ist.</td>
<td><code>var2 >= var1<br>
var1 >= 3</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Less_than_operator">Less than</a> (<code><</code>)</td>
<td>Gibt <code>true</code> zurück, wenn der linke Operand kleiner dem rechten Operanden ist.</td>
<td><code>var1 < var2<br>
"2" < 12</code></td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Less_than_or_equal_operator">Less than or equal</a> (<code><=</code>)</td>
<td>Gibt <code>true</code> zurück, wenn der linke Operand kleiner als, oder gleich dem rechten Operanden ist.</td>
<td><code>var1 <= var2<br>
var2 <= 5</code></td>
</tr>
</tbody>
</table>
<div class="note">
<p><strong>Note: </strong>(<strong>=></strong>) ist kein Operator, hiermit werden <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a> notiert.</p>
</div>
<h3 id="Arithmetische_Operatoren_(Rechenzeichen)">Arithmetische Operatoren (Rechenzeichen)</h3>
<p>Ein <a href="/de/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators">arithmetischer Operator</a> nimmt numerische Werte (Literale oder Variablen) als Operanden entgegen und gibt einen einzelnen numerischen Wert zurück. Die arithmetischen Standardoperatoren sind Addition (<code>+)</code>, Subtraktion (<code>-</code>), Multiplikation (<code>*</code>) und Division (<code>/</code>). Diese Operatoren funktionieren wie in den meisten anderen Programmiersprachen, wenn sie mit Fließkommazahlen verwendet werden (beachten Sie insbesondere, dass die Division durch Null {{jsxref ("Infinity")}} ergibt).</p>
<pre class="brush: js">1 / 2; // 0.5
1 / 2 == 1.0 / 2.0; // this is true
</pre>
<p>Neben den arithmetischen Standardoperatoren (+, -, * /), stellt JavaScript noch weitere Rechenzeichen zur Verfügung. Diese werden in der folgenden Tabelle aufgeführt:</p>
<table class="fullwidth-table">
<caption>Arithmetische Operatoren</caption>
<thead>
<tr>
<th scope="col">Operator</th>
<th scope="col">Beschreibung</th>
<th scope="col">Beispiel</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder">Remainder</a> (<code>%</code>)</td>
<td>Binärer Operator. Gibt den ganzzahligen Rest der Division beider Operanden zurück.</td>
<td><code>12 % 5</code> gibt <code>2</code> zurück.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment">Increment</a> (<code>++</code>)</td>
<td>
<p>Unärer Operator. Addiert 1 zu seinem Operanden.</p>
<p>Wenn der Operator vorangestellt wird (<code>++x</code>), gibt er den Wert seines Operanden zurück nachdem 1 addiert wurde; Wenn der Operator nachgestellt wird (<code>x++</code>), gibt er den Wert seines Operanden zurück, bevor 1 addiert wurde.</p>
</td>
<td>Wenn <code>x</code> gleich 3 ist, setzt <code>++x</code> <code>x</code> auf 4 und gibt 4 zurück, wobei <code>x++</code> 3 zurückgibt und erst danach <code>x</code> auf 4 setzt.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement">Decrement</a> (<code>--</code>)</td>
<td>
<p>Unärer Operator. Subtrahiert 1 von seinem Operanden.</p>
<p>Der Rückgabewert verhält sich analog zum increment Operator.</p>
</td>
<td>Wenn <code>x</code> gleich 3 ist, setzt <code>--x</code> <code>x</code> auf 2 und gibt 2 zurück, wobei <code>x--</code> 3 zurückgibt und erst danach, <code>x</code> auf 2 setzt.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_negation">Unary negation</a> (<code>-</code>)</td>
<td>Unärer Operator. Gibt die Negierung seines Operanden zurück.</td>
<td>Wenn <code>x</code> gleich 3 ist, gibt <code>-x</code> -3 zurück.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus">Unary plus</a> (<code>+</code>)</td>
<td>Versucht, den Operanden in eine Zahl umzuwandeln, wenn dies nicht bereits der Fall ist.</td>
<td><code>+"3"</code> gibt <code>3</code> zurück.<br>
<code>+true</code> gibt <code>1</code> zurück.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation">Exponentiation operator</a> (<code>**</code>) {{experimental_inline}}</td>
<td>Calculates the <code>base</code> to the <code>exponent</code> power, that is, <code>base<sup>exponent</sup></code></td>
<td><code>2 ** 3</code> gibt <code>8</code> zurück.<br>
<code>10 ** -1</code> gibt <code>0.1 </code>zurück.</td>
</tr>
</tbody>
</table>
<h3 id="Bitweise_Operatoren">Bitweise Operatoren</h3>
<p>Ein bitweiser Operator behandelt seine Operanden als eine Menge von 32 Bits (Nullen und Einsen) und nicht als dezimale, hexadezimale oder oktale Zahlen. Zum Beispiel hat die Dezimalzahl Neun eine binäre Darstellung von <code>1001</code>. Bitweise Operatoren führen ihre Operationen mit solchen binären Darstellungen aus, doch sie geben standardmäßige numerische JavaScript-Werte zurück.</p>
<p>Die folgende Tabelle fasst die bitweisen Operatoren von JavaScript zusammen.</p>
<table class="standard-table">
<caption>Bitweise Operatoren</caption>
<thead>
<tr>
<th scope="col">Operator</th>
<th scope="col">Verwendung</th>
<th scope="col">Beschreibung</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_AND">Bitwise AND</a></td>
<td><code>a & b</code></td>
<td>Returns a one in each bit position for which the corresponding bits of both operands are ones.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR">Bitwise OR</a></td>
<td><code>a | b</code></td>
<td>Returns a zero in each bit position for which the corresponding bits of both operands are zeros.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR">Bitwise XOR</a></td>
<td><code>a ^ b</code></td>
<td>Returns a zero in each bit position for which the corresponding bits are the same.<br>
[Returns a one in each bit position for which the corresponding bits are different.]</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT">Bitwise NOT</a></td>
<td><code>~ a</code></td>
<td>Inverts the bits of its operand.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Left_shift">Left shift</a></td>
<td><code>a << b</code></td>
<td>Shifts <code>a</code> in binary representation <code>b</code> bits to the left, shifting in zeros from the right.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Right_shift">Sign-propagating right shift</a></td>
<td><code>a >> b</code></td>
<td>Shifts <code>a</code> in binary representation <code>b</code> bits to the right, discarding bits shifted off.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Unsigned_right_shift">Zero-fill right shift</a></td>
<td><code>a >>> b</code></td>
<td>Shifts <code>a</code> in binary representation <code>b</code> bits to the right, discarding bits shifted off, and shifting in zeros from the left.</td>
</tr>
</tbody>
</table>
<h4 id="Bitwise_Logical_Operators" name="Bitwise_Logical_Operators">Bitweise logische Operatoren</h4>
<p>Konzeptuell arbeiten bitweise logische Operatoren wie folgt:</p>
<ul>
<li>Die Operanden werden zu 32 Bit Integer Zahlen konvertiert und als Folge von Bits (Nullen und Einsen) dargestellt. Zahlen mit mehr als 32 Bits verlieren dabei ihre signifikanten Bits bis auf eine Länge von 32. Im folgenden Beispiel wird eine Zahl mit mehr als 32 Bits zu einer 32 Bit Integer Zahl konvertiert:
<pre>Before: 11100110111110100000000000000110000000000001
After: 10100000000000000110000000000001</pre>
</li>
<li>Jedes Bit des ersten Operanden wird mit dem korrespondierenden Bit im zweiten Operanden gepaart: Erstes Bit mit dem ersten Bit, zweites Bit mit dem zweiten Bit und so weiter.</li>
<li>Der Operator wird auf jedes paar von Bits angewendet und das Ergebnis bitweise erstellt.</li>
</ul>
<p>Zum Beispiel ist die binäre Darstellung der Zahl neun 1001, die binäre Darstellung der Zahl 15 ist 1111. Wenn die bitweisen logischen Operatoren auf diese Zahlen angewendet werden, ergeben sich folgende Ergebnisse:</p>
<table class="standard-table">
<caption>Beispiele für bitweise Operatoren</caption>
<thead>
<tr>
<th scope="col">Ausdruck</th>
<th scope="col">Ergebnis</th>
<th scope="col">Binäre Darstellung</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>15 & 9</code></td>
<td><code>9</code></td>
<td><code>1111 & 1001 = 1001</code></td>
</tr>
<tr>
<td><code>15 | 9</code></td>
<td><code>15</code></td>
<td><code>1111 | 1001 = 1111</code></td>
</tr>
<tr>
<td><code>15 ^ 9</code></td>
<td><code>6</code></td>
<td><code>1111 ^ 1001 = 0110</code></td>
</tr>
<tr>
<td><code>~15</code></td>
<td><code>-16</code></td>
<td><code>~</code><code>00000000...</code><code>00001111 = </code><code>1111</code><code>1111</code><code>...</code><code>11110000</code></td>
</tr>
<tr>
<td><code>~9</code></td>
<td><code>-10</code></td>
<td><code>~</code><code>00000000</code><code>...</code><code>0000</code><code>1001 = </code><code>1111</code><code>1111</code><code>...</code><code>1111</code><code>0110</code></td>
</tr>
</tbody>
</table>
<p>Beachte, dass alle 32 Bits invertiert werden, wenn der bitweise NOT Operator benutzt wird. Wenn dabei das höchstwertigste (ganz linke) Bit auf 1 gesetzt wird, entsteht eine negative Zahl. Note that all 32 bits are inverted using the Bitwise NOT operator, and that values with the most significant (left-most) bit set to 1 represent negative numbers (Zweierkomplement). <code>~x</code> wird also genauso ausgewertet wie <code>-x - 1</code>.</p>
<h4 id="Bitwise_Shift_Operators" name="Bitwise_Shift_Operators">Bitweise Schiebeoperatoren</h4>
<p>Die bitweisen Schiebeoperatoren erwarten zwei Operanden. Der erste ist der Wert, der geschoben werden soll, der zweite die Anzahl der Bits, um die geschoben werden soll. Die Richtung, in die geschoben wird, wird durch den verwendeten Operator bestimmt.</p>
<p>Schiebeoperatoren konvertieren ihre Operanden in 32 Bit Integer Zahlen und liefern als Ergebnis einen Wert vom selben Typen wir der linke Operand.</p>
<p>Die Schiebeoperatoren sind in der folgenden Tabelle aufgelistet.</p>
<table class="fullwidth-table">
<caption>Bitwise shift operators</caption>
<thead>
<tr>
<th scope="col">Operator</th>
<th scope="col">Description</th>
<th scope="col">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#<<_(Left_shift)">Left shift</a><br>
(<code><<</code>)</td>
<td>This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.</td>
<td><code>9<<2</code> yields 36, because 1001 shifted 2 bits to the left becomes 100100, which is 36.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#>>_(Sign-propagating_right_shift)">Sign-propagating right shift</a> (<code>>></code>)</td>
<td>This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.</td>
<td><code>9>>2</code> yields 2, because 1001 shifted 2 bits to the right becomes 10, which is 2. Likewise, <code>-9>>2</code> yields -3, because the sign is preserved.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#>>>_(Zero-fill_right_shift)">Zero-fill right shift</a> (<code>>>></code>)</td>
<td>This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.</td>
<td><code>19>>>2</code> yields 4, because 10011 shifted 2 bits to the right becomes 100, which is 4. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result.</td>
</tr>
</tbody>
</table>
<h3 id="Logische_Operatoren">Logische Operatoren</h3>
<p><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators">Logische Operatoren</a> werden normalerweise mit boolesche (logischen) Werten verwendet - hierbei geben sie dann einen booleschen Wert zurück. Die Operatoren <code>&&</code> und <code>||</code> geben den Wert von einem der Operatoren zurück, sodass sie im Falle der Verwendung mit einem nicht-booleschen Wert auch einen nicht-booleschen Wert zurückgeben können. Die logischen Operatoren werden in der folgenden Tabelle beschrieben: </p>
<table class="fullwidth-table">
<caption>Logische Operatoren</caption>
<thead>
<tr>
<th scope="col">Operator</th>
<th scope="col">Verwendung</th>
<th scope="col">Beschreibung</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_AND">Logical AND</a><code> </code>(<code>&&</code>)</td>
<td><code>expr1 && expr2</code></td>
<td>Gibt <code>expr1</code> zurück, sofern es zu <code>false</code> konvertiert werden kann; ansonsten wird <code>expr2</code> zurückgegeben. Insofern mit booleschen Werten verwendet, <code>&&</code> gibt <code>true</code> zurück, wenn beide Operanden wahr sind; ansonsten <code>false</code>.</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR">Logical OR </a>(<code>||</code>)</td>
<td><code>expr1 || expr2</code></td>
<td>
<p>Gibt <code>expr1</code> zurück, sofern er zu <code>true</code> konvertiert werden kann. Insofern mit booleschen Werten verwendet, gibt der Operator <code>||</code> true zurück, wenn einer von beiden Operanden <code>true</code> ist; wenn beide <code>false</code> sind, wird <code>false</code> zurückgegeben. </p>
</td>
</tr>
<tr>
<td><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_NOT">Logical NOT </a>(<code>!</code>)</td>
<td><code>!expr</code></td>
<td>
<p>Gibt <code>false</code> zurück, wenn sein einziger Operand in <code>true</code> konvertiert werden kann; andernfalls gibt er <code>true</code> zurück. </p>
</td>
</tr>
</tbody>
</table>
<p>Beispiele von Ausdrücken, die in <code>false</code> umgewandelt werden können, sind solche, die <code>null</code>, 0, <code>NaN</code>, einen leeren String ("") oder <code>undefined</code> sind. </p>
<p>Die folgenden Zeilen zeigen Beispiele des <code>&&</code> (logisches UND) Operators.</p>
<pre class="brush: js">var a1 = true && true; // t && t returns true
var a2 = true && false; // t && f returns false
var a3 = false && true; // f && t returns false
var a4 = false && (3 == 4); // f && f returns false
var a5 = "Cat" && "Dog"; // t && t returns Dog
var a6 = false && "Cat"; // f && t returns false
var a7 = "Cat" && false; // t && f returns false
</pre>
<p>Die folgenden Zeilen zeigen Beispiele des <code>||</code> (logisches ODER) Operators:</p>
<pre class="brush: js">var o1 = true || true; // t || t returns true
var o2 = false || true; // f || t returns true
var o3 = true || false; // t || f returns true
var o4 = false || (3 == 4); // f || f returns false
var o5 = "Cat" || "Dog"; // t || t returns Cat
var o6 = false || "Cat"; // f || t returns Cat
var o7 = "Cat" || false; // t || f returns Cat
</pre>
<p>Die folgenden Zeilen zeigen Beispiele des <code>!</code> (logisches NICHT) Operators:</p>
<pre class="brush: js">var n1 = !true; // !t returns false
var n2 = !false; // !f returns true
var n3 = !"Cat"; // !t returns false
</pre>
<h4 id="Short-Circuit_Evaluation" name="Short-Circuit_Evaluation">Short-circuit-Bewertung </h4>
<p>Da logische Ausdrücke von links nach rechts bewertet werden, werden sie auf eine mögliche "Abkürzung" (short-circuit) hin gemäß den folgenden Regeln evaluiert:</p>
<ul>
<li><code>false</code> && <em>irgendwas</em> wird abgekürzt als <code>false</code> bewertet.</li>
<li><code>true</code> || <em>irgendwas</em> wird abgekürtz als <code>true</code> bewertet. </li>
</ul>
<p>Die Regeln der Logik garantieren, dass diese Bewertungen immer korrekt sind. Der <em>irgendwas</em>-Operand werden in den o.g. Beispielen hierbei nicht bewertet. </p>
<h3 id="String-Operatoren">String-Operatoren</h3>
<p>In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.</p>
<p>For example,</p>
<pre class="brush: js">console.log("my " + "string"); // console logs the string "my string".</pre>
<p>The shorthand assignment operator += can also be used to concatenate strings.</p>
<p>For example,</p>
<pre class="brush: js">var mystring = "alpha";
mystring += "bet"; // evaluates to "alphabet" and assigns this value to mystring.</pre>
<h3 id="Conditional_(ternary)_operator">Conditional (ternary) operator</h3>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator">conditional operator</a> is the only JavaScript operator that takes three operands. The operator can have one of two values based on a condition. The syntax is:</p>
<pre class="syntaxbox"><em>condition</em> ? <em>val1</em> : <em>val2</em>
</pre>
<p>If <code>condition</code> is true, the operator has the value of <code>val1</code>. Otherwise it has the value of <code>val2</code>. You can use the conditional operator anywhere you would use a standard operator.</p>
<p>For example,</p>
<pre class="brush: js">var status = (age >= 18) ? "adult" : "minor";
</pre>
<p>This statement assigns the value "adult" to the variable <code>status</code> if <code>age</code> is eighteen or more. Otherwise, it assigns the value "minor" to <code>status</code>.</p>
<h3 id="Comma_operator" name="Comma_operator">Comma operator</h3>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator">comma operator</a> (<code>,</code>) simply evaluates both of its operands and returns the value of the last operand. This operator is primarily used inside a <code>for</code> loop, to allow multiple variables to be updated each time through the loop.</p>
<p>For example, if <code>a</code> is a 2-dimensional array with 10 elements on a side, the following code uses the comma operator to update two variables at once. The code prints the values of the diagonal elements in the array:</p>
<pre class="brush: js">var x = [0,1,2,3,4,5,6,7,8,9];
var a = [x, x, x, x, x]
for (var i = 0, j = 9; i <= j; i++, j--)
console.log("a[" + i + "][" + j + "]= " + a[i][j]);
</pre>
<h3 id="Unary_operators">Unary operators</h3>
<p>A unary operation is an operation with only one operand.</p>
<h4 id="delete" name="delete"><code>delete</code></h4>
<p>The <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete</a></code> operator deletes an object, an object's property, or an element at a specified index in an array. The syntax is:</p>
<pre class="brush: js">delete objectName;
delete objectName.property;
delete objectName[index];
delete property; // legal only within a with statement
</pre>
<p>where <code>objectName</code> is the name of an object, <code>property</code> is an existing property, and <code>index</code> is an integer representing the location of an element in an array.</p>
<p>The fourth form is legal only within a <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/with">with</a></code> statement, to delete a property from an object.</p>
<p>You can use the <code>delete</code> operator to delete variables declared implicitly but not those declared with the <code>var</code> statement.</p>
<p>If the <code>delete</code> operator succeeds, it sets the property or element to <code>undefined</code>. The <code>delete</code> operator returns true if the operation is possible; it returns <code>false</code> if the operation is not possible.</p>
<pre class="brush: js">x = 42;
var y = 43;
myobj = new Number();
myobj.h = 4; // create property h
delete x; // returns true (can delete if declared implicitly)
delete y; // returns false (cannot delete if declared with var)
delete Math.PI; // returns false (cannot delete predefined properties)
delete myobj.h; // returns true (can delete user-defined properties)
delete myobj; // returns true (can delete if declared implicitly)
</pre>
<h5 id="Deleting_array_elements">Deleting array elements</h5>
<p>When you delete an array element, the array length is not affected. For example, if you delete <code>a[3]</code>, <code>a[4]</code> is still <code>a[4]</code> and <code>a[3]</code> is undefined.</p>
<p>When the <code>delete</code> operator removes an array element, that element is no longer in the array. In the following example, <code>trees[3]</code> is removed with <code>delete</code>. However, <code>trees[3]</code> is still addressable and returns <code>undefined</code>.</p>
<pre class="brush: js">var trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
if (3 in trees) {
// this does not get executed
}
</pre>
<p>If you want an array element to exist but have an undefined value, use the <code>undefined</code> keyword instead of the <code>delete</code> operator. In the following example, <code>trees[3]</code> is assigned the value <code>undefined</code>, but the array element still exists:</p>
<pre class="brush: js">var trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees[3] = undefined;
if (3 in trees) {
// this gets executed
}
</pre>
<h4 id="typeof" name="typeof"><code>typeof</code></h4>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof"><code>typeof</code> operator</a> is used in either of the following ways:</p>
<pre class="syntaxbox">typeof operand
typeof (operand)
</pre>
<p>The <code>typeof</code> operator returns a string indicating the type of the unevaluated operand. <code>operand</code> is the string, variable, keyword, or object for which the type is to be returned. The parentheses are optional.</p>
<p>Suppose you define the following variables:</p>
<pre class="brush: js">var myFun = new Function("5 + 2");
var shape = "round";
var size = 1;
var today = new Date();
</pre>
<p>The <code>typeof</code> operator returns the following results for these variables:</p>
<pre class="brush: js">typeof myFun; // returns "function"
typeof shape; // returns "string"
typeof size; // returns "number"
typeof today; // returns "object"
typeof dontExist; // returns "undefined"
</pre>
<p>For the keywords <code>true</code> and <code>null</code>, the <code>typeof</code> operator returns the following results:</p>
<pre class="brush: js">typeof true; // returns "boolean"
typeof null; // returns "object"
</pre>
<p>For a number or string, the <code>typeof</code> operator returns the following results:</p>
<pre class="brush: js">typeof 62; // returns "number"
typeof 'Hello world'; // returns "string"
</pre>
<p>For property values, the <code>typeof</code> operator returns the type of value the property contains:</p>
<pre class="brush: js">typeof document.lastModified; // returns "string"
typeof window.length; // returns "number"
typeof Math.LN2; // returns "number"
</pre>
<p>For methods and functions, the <code>typeof</code> operator returns results as follows:</p>
<pre class="brush: js">typeof blur; // returns "function"
typeof eval; // returns "function"
typeof parseInt; // returns "function"
typeof shape.split; // returns "function"
</pre>
<p>For predefined objects, the <code>typeof</code> operator returns results as follows:</p>
<pre class="brush: js">typeof Date; // returns "function"
typeof Function; // returns "function"
typeof Math; // returns "object"
typeof Option; // returns "function"
typeof String; // returns "function"
</pre>
<h4 id="void" name="void"><code>void</code></h4>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/void"><code>void</code> operator</a> is used in either of the following ways:</p>
<pre class="syntaxbox">void (expression)
void expression
</pre>
<p>The <code>void</code> operator specifies an expression to be evaluated without returning a value. <code>expression</code> is a JavaScript expression to evaluate. The parentheses surrounding the expression are optional, but it is good style to use them.</p>
<p>You can use the <code>void</code> operator to specify an expression as a hypertext link. The expression is evaluated but is not loaded in place of the current document.</p>
<p>The following code creates a hypertext link that does nothing when the user clicks it. When the user clicks the link, <code>void(0)</code> evaluates to <code>undefined</code>, which has no effect in JavaScript.</p>
<pre class="brush: html"><a>Click here to do nothing</a>
</pre>
<p>The following code creates a hypertext link that submits a form when the user clicks it.</p>
<pre class="brush: html"><a>
Click here to submit</a></pre>
<h3 id="Relational_operators">Relational operators</h3>
<p>A relational operator compares its operands and returns a <code>Boolean</code> value based on whether the comparison is true.</p>
<h4 id="in"><code>in</code></h4>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/in"><code>in</code> operator</a> returns true if the specified property is in the specified object. The syntax is:</p>
<pre class="brush: js">propNameOrNumber in objectName
</pre>
<p>where <code>propNameOrNumber</code> is a string or numeric expression representing a property name or array index, and <code>objectName</code> is the name of an object.</p>
<p>The following examples show some uses of the <code>in</code> operator.</p>
<pre class="brush: js">// Arrays
var trees = ["redwood", "bay", "cedar", "oak", "maple"];
0 in trees; // returns true
3 in trees; // returns true
6 in trees; // returns false
"bay" in trees; // returns false (you must specify the index number,
// not the value at that index)
"length" in trees; // returns true (length is an Array property)
// built-in objects
"PI" in Math; // returns true
var myString = new String("coral");
"length" in myString; // returns true
// Custom objects
var mycar = { make: "Honda", model: "Accord", year: 1998 };
"make" in mycar; // returns true
"model" in mycar; // returns true
</pre>
<h4 id="instanceof" name="instanceof"><code>instanceof</code></h4>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> operator</a> returns true if the specified object is of the specified object type. The syntax is:</p>
<pre class="syntaxbox">objectName instanceof objectType
</pre>
<p>where <code>objectName</code> is the name of the object to compare to <code>objectType</code>, and <code>objectType</code> is an object type, such as {{jsxref("Date")}} or {{jsxref("Array")}}.</p>
<p>Use <code>instanceof</code> when you need to confirm the type of an object at runtime. For example, when catching exceptions, you can branch to different exception-handling code depending on the type of exception thrown.</p>
<p>For example, the following code uses <code>instanceof</code> to determine whether <code>theDay</code> is a <code>Date</code> object. Because <code>theDay</code> is a <code>Date</code> object, the statements in the <code>if</code> statement execute.</p>
<pre class="brush: js">var theDay = new Date(1995, 12, 17);
if (theDay instanceof Date) {
// statements to execute
}
</pre>
<h3 id="Operator_precedence">Operator precedence</h3>
<p>The <em>precedence</em> of operators determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses.</p>
<p>The following table describes the precedence of operators, from highest to lowest.</p>
<table class="standard-table">
<caption>Operator precedence</caption>
<thead>
<tr>
<th scope="col">Operator type</th>
<th scope="col">Individual operators</th>
</tr>
</thead>
<tbody>
<tr>
<td>member</td>
<td><code>. []</code></td>
</tr>
<tr>
<td>call / create instance</td>
<td><code>() new</code></td>
</tr>
<tr>
<td>negation/increment</td>
<td><code>! ~ - + ++ -- typeof void delete</code></td>
</tr>
<tr>
<td>multiply/divide</td>
<td><code>* / %</code></td>
</tr>
<tr>
<td>addition/subtraction</td>
<td><code>+ -</code></td>
</tr>
<tr>
<td>bitwise shift</td>
<td><code><< >> >>></code></td>
</tr>
<tr>
<td>relational</td>
<td><code>< <= > >= in instanceof</code></td>
</tr>
<tr>
<td>equality</td>
<td><code>== != === !==</code></td>
</tr>
<tr>
<td>bitwise-and</td>
<td><code>&</code></td>
</tr>
<tr>
<td>bitwise-xor</td>
<td><code>^</code></td>
</tr>
<tr>
<td>bitwise-or</td>
<td><code>|</code></td>
</tr>
<tr>
<td>logical-and</td>
<td><code>&&</code></td>
</tr>
<tr>
<td>logical-or</td>
<td><code>||</code></td>
</tr>
<tr>
<td>conditional</td>
<td><code>?:</code></td>
</tr>
<tr>
<td>assignment</td>
<td><code>= += -= *= /= %= <<= >>= >>>= &= ^= |=</code></td>
</tr>
<tr>
<td>comma</td>
<td><code>,</code></td>
</tr>
</tbody>
</table>
<p>A more detailed version of this table, complete with links to additional details about each operator, may be found in <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table">JavaScript Reference</a>.</p>
<h2 id="Expressions">Expressions</h2>
<p>An <em>expression</em> is any valid unit of code that resolves to a value.</p>
<p>Every syntactically valid expression resolves to some value but conceptually, there are two types of expressions: with side effects (for example: those that assign value to a variable) and those that in some sense evaluate and therefore resolve to a value.</p>
<p>The expression <code>x = 7</code> is an example of the first type. This expression uses the =<em> operator</em> to assign the value seven to the variable <code>x</code>. The expression itself evaluates to seven.</p>
<p>The code <code>3 + 4</code> is an example of the second expression type. This expression uses the + operator to add three and four together without assigning the result, seven, to a variable.<br>
<br>
JavaScript has the following expression categories:</p>
<ul>
<li>Arithmetic: evaluates to a number, for example 3.14159. (Generally uses {{ web.link("#Arithmetic_operators", "arithmetic operators") }}.)</li>
<li>String: evaluates to a character string, for example, "Fred" or "234". (Generally uses {{ web.link("#String_operators", "string operators") }}.)</li>
<li>Logical: evaluates to true or false. (Often involves {{ web.link("#Logical_operators", "logical operators") }}.)</li>
<li>Primary expressions: Basic keywords and general expressions in JavaScript.</li>
<li>Left-hand-side expressions: Left values are the destination of an assignment.</li>
</ul>
<h3 id="Primary_expressions">Primary expressions</h3>
<p>Basic keywords and general expressions in JavaScript.</p>
<h4 id="this" name="this"><code>this</code></h4>
<p>Use the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/this"><code>this</code> keyword</a> to refer to the current object. In general, <code>this</code> refers to the calling object in a method. Use <code>this</code> either with the dot or the bracket notation:</p>
<pre class="syntaxbox">this["propertyName"]
this.propertyName
</pre>
<p>Suppose a function called <code>validate</code> validates an object's <code>value</code> property, given the object and the high and low values:</p>
<pre class="brush: js">function validate(obj, lowval, hival){
if ((obj.value < lowval) || (obj.value > hival))
console.log("Invalid Value!");
}
</pre>
<p>You could call <code>validate</code> in each form element's <code>onChange</code> event handler, using <code>this</code> to pass it the form element, as in the following example:</p>
<pre class="brush: html"></pre>
<p>Enter a number between 18 and 99:</p>
<p><input></p>
<h4 id="Grouping_operator">Grouping operator</h4>
<p>The grouping operator <code>( )</code> controls the precedence of evaluation in expressions. For example, you can override multiplication and division first, then addition and subtraction to evaluate addition first.</p>
<pre class="brush:js">var a = 1;
var b = 2;
var c = 3;
// default precedence
a + b * c // 7
// evaluated by default like this
a + (b * c) // 7
// now overriding precedence
// addition before multiplication
(a + b) * c // 9
// which is equivalent to
a * c + b * c // 9
</pre>
<h4 id="Comprehensions">Comprehensions</h4>
<p>Comprehensions are an experimental JavaScript feature, targeted to be included in a future ECMAScript version. There are two versions of comprehensions:</p>
<dl>
<dt>{{experimental_inline}} {{jsxref("Operators/Array_comprehensions", "[for (x of y) x]")}}</dt>
<dd>Array comprehensions.</dd>
<dt>{{experimental_inline}} {{jsxref("Operators/Generator_comprehensions", "(for (x of y) y)")}}</dt>
<dd>Generator comprehensions.</dd>
</dl>
<p>Comprehensions exist in many programming languages and allow you to quickly assemble a new array based on an existing one, for example.</p>
<pre class="brush:js">[for (i of [ 1, 2, 3 ]) i*i ];
// [ 1, 4, 9 ]
var abc = [ "A", "B", "C" ];
[for (letter of abc) letter.toLowerCase()];
// [ "a", "b", "c" ]</pre>
<h3 id="Left-hand-side_expressions">Left-hand-side expressions</h3>
<p>Left values are the destination of an assignment.</p>
<h4 id="new" name="new"><code>new</code></h4>
<p>You can use the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a> to create an instance of a user-defined object type or of one of the built-in object types. Use <code>new</code> as follows:</p>
<pre class="brush: js">var objectName = new objectType([param1, param2, ..., paramN]);
</pre>
<h4 id="super">super</h4>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/super">super keyword</a> is used to call functions on an object's parent. It is useful with <a href="/en-US/docs/Web/JavaScript/Reference/Classes">classes</a> to call the parent constructor, for example.</p>
<pre class="syntaxbox">super([arguments]); // calls the parent constructor.
super.functionOnParent([arguments]);
</pre>
<h4 id="Spread_operator">Spread operator</h4>
<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a> allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.</p>
<p><strong>Example:</strong> Today if you have an array and want to create a new array with the existing one being part of it, the array literal syntax is no longer sufficient and you have to fall back to imperative code, using a combination of <code>push</code>, <code>splice</code>, <code>concat</code>, etc. With spread syntax this becomes much more succinct:</p>
<pre class="brush: js">var parts = ['shoulder', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];</pre>
<p>Similarly, the spread operator works with function calls:</p>
<pre class="brush: js">function f(x, y, z) { }
var args = [0, 1, 2];
f(...args);</pre>
<div>{{PreviousNext("Web/JavaScript/Guide/Funktionen", "Web/JavaScript/Guide/Numbers_and_dates")}}</div>
|