This translation is incomplete. Please help translate this article from English.
Bu sayfa JavaScript'in sözcük bazlı gramerini anlatır.This page describes JavaScript's lexical grammar. ECMAScript'in kaynak kodu soldan sağa taranır ve semboller, kontrol karakterleri, satır sonlandırıcılar, yorumlar ve boşluklar gibi bir dizi elemanlar dönüştürülür. ECMAScript also defines certain keywords and literals and has rules for automatic insertion of semicolons to end statements.
Kontrol karakterleri
Kontrol karakterlerinin görsel temsili yoktur fakat metni yorumlamada kullanılabilir.
Code point | İsim | Kısaltma | Tanım |
---|---|---|---|
U+200C |
Zero width non-joiner | <ZWNJ> | Placed between characters to prevent being connected into ligatures in certain languages (Wikipedia). |
U+200D |
Zero width joiner | <ZWJ> | Placed between characters that would not normally be connected in order to cause the characters to be rendered using their connected form in certain languages (Wikipedia). |
U+FEFF |
Byte order mark | <BOM> | Used at the start of the script to mark it as Unicode and the text's byte order (Wikipedia). |
White space
White space characters improve the readability of source text and separate tokens from each other. These characters are usually unnecessary for the functionality of the code. Minification tools are often used to remove whitespace in order to reduce the amount of data that needs to be transferred.
Code point | Name | Abbreviation | Description | Escape sequence |
---|---|---|---|---|
U+0009 | Character tabulation | <HT> | Horizontal tabulation | \t |
U+000B | Line tabulation | <VT> | Vertical tabulation | \v |
U+000C | Form feed | <FF> | Page breaking control character (Wikipedia). | \f |
U+0020 | Space | <SP> | Normal space | |
U+00A0 | No-break space | <NBSP> | Normal space, but no point at which a line may break | |
Others | Other Unicode space characters | <USP> | Spaces in Unicode on Wikipedia |
Satır Sonlandırıcı
In addition to white space characters, line terminator characters are used to improve the readability of the source text. However, in some cases, line terminators can influence the execution of JavaScript code as there are a few places where they are forbidden. Line terminators also affect the process of automatic semicolon insertion. Line terminators are matched by the \s class in regular expressions.
Only the following Unicode code points are treated as line terminators in ECMAScript, other line breaking characters are treated as white space (for example, Next Line, NEL, U+0085 is considered as white space).
Code point | Name | Abbreviation | Description | Escape sequence |
---|---|---|---|---|
U+000A | Line Feed | <LF> | New line character in UNIX systems. | \n |
U+000D | Carriage Return | <CR> | New line character in Commodore and early Mac systems. | \r |
U+2028 | Line Separator | <LS> | Wikipedia | |
U+2029 | Paragraph Separator | <PS> | Wikipedia |
Yorumlar
İpuçları, yorumlar, kod hataları ve öneriler için yorumlar kullanılır. Bu okumayı ve anlamayı kolaylaştırır. Bunun yanısıra çalışmasını istemediğiniz kod kısmını yorum gibi gösterip hata ayıklamada kolaylık sağlayabilirsiniz. Bu iyi bir hata ayıklama yöntemidir.
JavaScript'te kod satırları içine yorum eklemek için iki yöntem vardır. Çoğu proglamlama dili ile aynıdır.
İlki // -iki tane slash- ile yapılır; // koyduğunuz satırda // 'dan satırın sonuna kadar olan bütün kod yorum olarak atanır.Örneğin:
function comment() { // Bu tek satır bir JavaScript yorumudur. console.log("Merhaba Dünya!"); } comment();
İkinci yol, biraz daha esnek olan, /* ............ */ dizaynıdır. Noktalar ile gösterilen kısma satır limiti olmadan istediğiniz kadar yorum ekleyebilirsiniz. Baştaki /* ikilisi yorum satırının başladığını, sondaki */ ikilisi ise yorum satırının sonlandığını ifade eder. Başlattığınız yorumu sonlandırmazsanız, JavaScript kodlarınınızın geri kalanı işlevsiz kalır ve büyük ihtimalle JS hata mesajı verir.
Örnekler:
1-
function comment() { /* Bu bir tek satırlık JavaScript yorumudur. */ console.log("Hello world!"); } comment();
2-
function comment() { /* Bu iki satırlık bir JavaScript kodudur. Yorum olmasını istediğiniz yere kadar kapatmak zorunda değilsiniz. */ console.log("Merhaba Dünya!"); } comment();
3-Okunmasını zorlaştırmasına rağmen, satırın içinde de kullanabilirsiniz. Kodunuzun daha siz veya başkası tarafından okunmasını zorlaştıracağı için dikkatli kullanmalısınız:
function comment(x) { console.log("Merhaba " + x /* insert the value of x */ + " !"); } comment("Dünya");
Son olarak, bir kısım kodun çalışmasını engellemek için kullanabilirsiniz, iyi bir hata ayıklama deneyimidir:
function comment() { /* console.log("Merhaba Dünya!"); */ } comment();
console.log()
yorum içerisinde iken asla çağrılmaz. Daha fazla satır kodu işlevsiz bırakmak için de bu yolu kullanabilirsiniz.
Anahtar Kelimeler
Reserved keywords as of ECMAScript 6
Future reserved keywords
The following are reserved as future keywords by the ECMAScript specification. They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers. These keywords may not be used in either strict or non-strict mode.
enum
await
The following are reserved as future keywords when they are found in strict mode code:
implements
package
protected
static
interface
private
public
Future reserved keywords in older standards
The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).
abstract
boolean
byte
char
double
final
float
goto
int
long
native
short
synchronized
transient
volatile
Additionally, the literals null
, true
, and false
are reserved in ECMAScript for their normal uses.
Reserved word usage
Reserved words actually only apply to Identifiers (vs. IdentifierNames
) . As described in es5.github.com/#A.1, these are all IdentifierNames
which do not exclude ReservedWords
.
a.import a["import"] a = { import: "test" }.
On the other hand the following is illegal because it's an Identifier, which is an IdentifierName
without the reserved words. Identifiers are used for FunctionDeclaration
and FunctionExpression
.
function import() {} // Illegal.
Literals
Null literal
See also null
for more information.
null
Boolean literal
See also Boolean
for more information.
true false
Numeric literals
Decimal
1234567890 42 // Caution when using leading zeros: 0888 // 888 parsed as decimal 0777 // parsed as octal, 511 in decimal
Note that decimal literals can start with a zero (0
) followed by another decimal digit, but If the next digit after the leading 0
is smaller than 8, the number gets parsed as an octal number. This won't throw in JavaScript, see bug 957513. See also the page about parseInt()
.
Binary
Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (0b
or 0B
). Because this syntax is new in ECMAScript 6, see the browser compatibility table, below. If the digits after the 0b
are not 0 or 1, the following SyntaxError
is thrown: "Missing binary digits after 0b".
var FLT_SIGNBIT = 0b10000000000000000000000000000000; // 2147483648 var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040 var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607
Octal
Octal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (0o
or 0O)
. Because this syntax is new in ECMAScript 6, see the browser compatibility table, below. If the digits after the 0o
are outside the range (01234567), the following SyntaxError
is thrown: "Missing octal digits after 0o".
var n = 0O755; // 493 var m = 0o644; // 420 // Also possible with leading zeros (see note about decimals above) 0755 0644
Hexadecimal
Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x
or 0X)
. If the digits after 0x are outside the range (0123456789ABCDEF), the following SyntaxError
is thrown: "Identifier starts immediately after numeric literal".
0xFFFFFFFFFFFFFFFFF // 295147905179352830000 0x123456789ABCDEF // 81985529216486900 0XA // 10
Object literals
See also Object
and Object initializer for more information.
var o = { a: "foo", b: "bar", c: 42 }; // shorthand notation. New in ES6 var a = "foo", b = "bar", c = 42; var o = {a, b, c}; // instead of var o = { a: a, b: b, c: c };
Array literals
See also Array
for more information.
[1954, 1974, 1990, 2014]
String literals
'foo' "bar"
Hexadecimal escape sequences
'\xA9' // "©"
Unicode escape sequences
The Unicode escape sequences require at least four characters following \u
.
'\u00A9' // "©"
Unicode code point escapes
New in ECMAScript 6. With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to 0x10FFFF
. With simple Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same.
See also String.fromCodePoint()
or String.prototype.codePointAt()
.
'\u{2F804}' // the same with simple Unicode escapes '\uD87E\uDC04'
Regular expression literals
See also RegExp
for more information.
/ab+c/g // An "empty" regular expression literal // The empty non-capturing group is necessary // to avoid ambiguity with single-line comments. /(?:)/
Template literals
See also template strings for more information.
`string text` `string text line 1 string text line 2` `string text ${expression} string text` tag `string text ${expression} string text`
Automatic semicolon insertion
Some JavaScript statements must be terminated with semicolons and are therefore affected by automatic semicolon insertion (ASI):
- Empty statement
let
,const
, variable statementimport
,export
, module declaration- Expression statement
debugger
continue
,break
,throw
return
The ECMAScript specification mentions three rules of semicolon insertion.
1. A semicolon is inserted before, when a Line terminator or "}" is encountered that is not allowed by the grammar.
{ 1 2 } 3 // is transformed by ASI into { 1 2 ;} 3;
2. A semicolon is inserted at the end, when the end of the input stream of tokens is detected and the parser is unable to parse the single input stream as a complete program.
Here ++
is not treated as a postfix operator applying to variable b
, because a line terminator occurs between b
and ++
.
a = b ++c // is transformend by ASI into a = b; ++c;
3. A semicolon is inserted at the end, when a statement with restricted productions in the grammar is followed by a line terminator. These statements with "no LineTerminator here" rules are:
- PostfixExpressions (
++
and--
) continue
break
return
yield
,yield*
module
return a + b // is transformed by ASI into return; a + b;
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) The definition of 'Lexical Conventions' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Lexical Grammar' in that specification. |
Standard | Added: Binary and Octal Numeric literals, Unicode code point escapes, Templates |
ECMAScript 2016 Draft (7th Edition, ECMA-262) The definition of 'Lexical Grammar' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Binary and octal numeric literals ( 0b and 0o ) |
41 | 12 | 25 (25) | ? | 28 | 9 |
Unicode code point escapes ( \u{} ) |
44 | 12 | 40 (40) | Not supported | 31 | 9 |
Shorthand notation for object literals | 43 | 12 | 33 (33) | Not supported | 30 | 9 |
Template literals | 41 | 12 | 34 (34) | Not supported | 28 | 9 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Binary and octal numeric literals (0b and 0o ) |
? | 41 | 33.0 (33) | ? | ? | ? |
Unicode code point escapes (\u{} ) |
? | ? | 40.0 (40) | ? | ? | ? |
Shorthand notation for object literals | Not supported | Not supported | 33.0 (33) | Not supported | Not supported | Not supported |
Template literals | Not supported | Not supported | 34.0 (34) | Not supported | Not supported | Not supported |
Firefox-specific notes
- Prior to Firefox 5 (JavaScript 1.8.6), future reserved keywords could be used when not in strict mode. This ECMAScript violation was fixed in Firefox 5.