JavaScript variables are case sensitive. JavaScript syntax in simple words. Function declaration and function expression

JavaScript syntax is a set of rules for how JavaScript programs are created. In this lesson we will look at the basic lexical structures of the language.

Character set

When writing programs in JavaScript, the Unicode character set is used. Unlike 7-bit encoding, which is only suitable for English language, and an 8-bit encoding suitable only for English and major Western European languages, 16-bit Unicode supports virtually every written language on the planet. The ECMAScript v3 standard requires JavaScript implementations to support Unicode version 2.1 or higher, and the ECMAScript v5 standard requires implementations to support Unicode version 3 or higher.

Var str = "hello, world!"; // Latin alphabet is used var str = "Hello, world!"; // Use Cyrillic

Whitespace characters

Whitespace improves readability source code, but these symbols are generally not needed for js script functionality.

Whitespace characters Symbol code Name Abbreviation Description Escape sequence
U+0009 Horizontal tabulation Moves the print position to the next horizontal tab stop \t
U+000B Vertical tab Moves the print position to the next vertical tab stop \v
U+000C Page feed, page change Throws out the current page and starts printing from the next one \f
U+0020 Space Letter spacing
U+00A0 Non-breaking space A character that appears inside a string like regular space, but does not allow the line to be broken at this point

In addition to whitespace characters, end-of-line characters are also used to improve the readability of source text. However, in some cases, end-of-line characters can affect execution JavaScript code, since there are several moments when their use is prohibited. End of line characters also affect the process automatic installation semicolons.

The following characters are recognized by JavaScript interpreters as end-of-line characters:

Semicolon

A JavaScript program (script) is a list of “instructions” that are executed by a web browser.
In JavaScript, statements are typically separated by semicolons (;).

If several instructions are located on one line, then a semicolon (;) should be placed between them.

In many cases, JavaScript interprets a newline as a command separator to automatically enter semicolons (ASI) to terminate statements.
If each instruction is placed on a separate line, then the separator can be omitted:

One instruction can be located on several lines:

In this case, JavaScript waits for the expression to complete and therefore does not automatically insert a “virtual” semicolon between the lines.

Note: Although semicolons are optional at the end of statements, it is recommended that you always add them. Now this is a rule that all large projects follow.

This rule prevents some errors, such as uncompleted input, and also allows you to compress code by removing white space. Compressing code without semicolons results in syntax errors. In addition, the presence of semicolons prevents performance degradation because parsers try to correct suspected errors by adding missing semicolons.

Case sensitivity

JavaScript programs are written using the Unicode character set, which includes the ASCII and Latin-1 character sets and is supported by almost all languages ​​and platforms.
In JavaScript, all elements, including variable, function, and operator names, are case sensitive and must always contain the same set of uppercase and lowercase characters. lowercase letters. For example, the while keyword should be typed as "while" rather than "While" or "WHILE".

Similarly, num, NUM and Num are three different variables:

Comments

Comments allow you to highlight a fragment of a program that is not executed by the JavaScript interpreter, but serves only to explain the content of the program.
Comments in JS can be single-line or multi-line.

Single-line comments begin with a double slash // . The text is considered a comment until the end of the line:

A multi-line comment begins with a slash and an asterisk (/*) and ends with them in reverse order (*/). This way you can comment out one or more lines:

Tip: Don't neglect comments in your codes. They will be useful to you when debugging and maintaining programs. At the development stage, it may be better to comment out an unnecessary fragment of a program than to simply delete it. What if it has to be restored?

Identifiers

An identifier is a sequence of letters, numbers, underscores (_), and dollar signs ($). A number cannot be the first character of an identifier because it would make it harder for the JavaScript interpreter to distinguish identifiers from numbers. Identifiers act as names of variables, functions, object properties, etc.
For compatibility and ease of editing, only ASCII characters and numbers are typically used to compose identifiers. However, in ECMAScript v3, identifiers can contain letters and numbers from full set Unicode characters. This allows programmers to name variables in their native languages ​​and use mathematical symbols in them:

Var name = "Max"; var Π = 3.14;

Historically, programmers have used different ways combining multiple words to record identifiers. Today there are two established unspoken styles: camelCase and snake_case.
In JavaScript, the most popular naming style for multi-word identifiers is camelCase. This means that the first letter is lowercase, and the first letters of all subsequent words are uppercase, for example:

Var firstSecond; var myCar = "audi"; var doSomethingImportant;

Note: In JavaScript, combining multiple words to write identifiers using hyphens is not allowed. They are reserved for mathematical subtractions.

Note: In JavaScript, keywords, reserved words, and the values ​​true , false , and null cannot be identifiers.

Keywords and reserved words

The ECMA-262 standard defines a set of keywords that cannot be used as identifiers. Reserved words have a specific meaning in JavaScript because they are part of the language's syntax. Using reserved words will result in a compilation error when loading the script.

Reserved keywords according to ECMAScript® 2015 version
  • break
  • catch
  • class
  • const
  • continue
  • debugger
  • default
  • delete
  • export
  • extends
  • finally
  • function
  • import
  • instanceof
  • return
  • super
  • switch
  • throw
  • typeof
  • while
  • yield
Keywords reserved for future use

In addition, ECMA-262 contains a set of reserved words, which also cannot be used as identifiers or property names. There is currently no functionality behind them, but it may appear in future versions:

  • await

In strict mode, the following words are added to this list:

  • implements
  • package
  • protected
  • static
  • interface
  • private
  • public
Reserved keywords in ECMAScript® versions 1 to 3
  • abstract
  • boolean
  • double
  • final
  • float
  • native
  • short
  • synchronized
  • transient
  • volatile

In the 5th edition of ECMAScript, the rules for using keywords and reserved words have been slightly changed. As before, they cannot be identifiers, but now they can be used as property names in objects. However, to ensure compatibility with past and future editions of ECMAScript, it is still best not to use keywords and reserved words as identifiers and property names.

Results
  • The JavaScript interpreter ignores any whitespace that may be present between language constructs and treats the program text as a continuous stream of code.
    Additionally, JavaScript also, for the most part, ignores newline characters. Therefore, spaces and newlines can be used without restrictions in the source code of programs to format and give them a readable appearance.
  • Omitting semicolons is not considered good programming practice, and it is therefore advisable to develop the habit of using them.
  • In JavaScript, all elements, including variable, function, and operator names, are case sensitive and must always contain the same set of uppercase and lowercase letters.
  • Don't neglect comments in your codes. They will be useful to you when debugging and maintaining programs. Don't worry about increasing the code size because... There are JavaScript compression tools that will easily remove comments when published.
  • Identifiers act as names of variables, functions, object properties and consist of a sequence of letters, numbers, underscores (_) and dollar signs ($) .
  • JavaScript keywords used to denote elements of language syntax, as well as other words reserved for future use, cannot be used as names of variables, functions, or objects.

JavaScript can be implemented using JavaScript statements, which are placed in HTML tags script ... /script on a web page.

You can place script tags containing your JavaScript anywhere on your web page, but it's generally recommended to store it in head tags.

The script tag alerts the browser program to begin interpreting all text between these tags as script. The simple syntax for your JavaScript would look like this.

JavaScript code

The script tag contains two important attributes -

  • Language - This attribute indicates what scripting language you are using. Typically its value will be javascript. Although recent versions of HTML (and XHTML, its successor) have stopped using this attribute.
  • Type. This attribute is now recommended to indicate the scripting language used and its value should be set to "text/javascript".

So your JavaScript segment would look like this:

JavaScript code

Your first JavaScript script

Let's take a printable example " Hello World" We've added an extra HTML comment that surrounds our JavaScript code. This is to save our code from a browser that doesn't support JavaScript. The comment ends with "// ->". Here "//" means a comment in JavaScript, so we add this to prevent the browser from reading the end of the HTML comment as part of the JavaScript code. We then call the document.write function, which writes a string to our HTML document.

This function can be used to write text, HTML, or both. Take a look at the following code.

This code will give the following result:

Hello World!

Spaces and line breaks

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs. You can freely use spaces, tabs, and newlines in your program, and you can format and indent your programs in a neat and consistent manner, making your code easy to read and understand.

Semicolons in JavaScript

IN simple instructions in JavaScript it is usually followed by a semicolon, as in C, C++ and Java. JavaScript, however, allows you to skip this semicolon if you put each of your statements on a separate line. For example, the following code could be written without semicolons.

But when formatting in one line like this, you have to use semicolons -

Note. It is good programming practice to use semicolons.

Case sensitivity

JavaScript is a case-sensitive language. This means that keywords, variables, function names, and any other identifiers must always be entered with a consistent capital letter.

So the Time and TIME identifiers will convey different meanings to JavaScript.

NOTE. You should be careful when writing variable and function names in JavaScript.

JavaScript Syntax

Since 1995, JavaScript has come a long way from a humble component of the Netscape browser to today's high-performance JIT interpreters. It would seem that just five years ago developers were stunned by the emergence of Ajax, and now complex JavaScript applications have reached volumes of hundreds and thousands of lines of code.

Last year, a new generation of JavaScript applications appeared that looked no different from desktop applications, an incredible advancement in web technology. Gone are slow page requests every time a user interacts with the application. JavaScript engines have become so powerful that it is possible to save state on the client side, which significantly speeds up the response of the application and improves the quality of its performance.

If you know other programming languages, you may find it helpful to know that JavaScript is a high-level, dynamic, untyped, and interpreted programming language that is well suited for object-oriented and functional programming styles. JavaScript inherits its syntax from the Java language, its first-class functions from the Scheme language, and its prototype-based inheritance mechanism from the Self language. But you don't need to know all of these languages ​​or be familiar with their terminology to learn JavaScript.

In this article we will look at the basic lexical structures of the language.

Symbols

When writing programs in JavaScript, the Unicode character set is used. Unicode is a superset of ASCII and Latin-1 and supports virtually every written language on the planet. The ECMAScript 3 standard requires JavaScript implementations to support Unicode version 2.1 or later, and the ECMAScript 5 standard requires implementations to support Unicode version 3 or later.

Case sensitivity

JavaScript is a case-sensitive language. This means that keywords, variable and function names, and any other language identifiers must always contain the same set of uppercase and lowercase letters.

For example, the while keyword should be typed as "while" rather than "While" or "WHILE". Similarly, myvar, Myvar, MyVar and MYVAR are the names of four different variables. Note, however, that HTML markup language (unlike XHTML) is not case sensitive. Since HTML and client-side JavaScript are closely related, this distinction can be confusing. Many JavaScript objects and their properties have the same names as tags and attributes HTML language which they represent. However, while in HTML these tags and attributes can be typed in any case, in JavaScript they usually must be typed in lowercase letters.

For example, the onclick attribute of an event handler is most often specified as onClick in HTML, but in JavaScript code (or an XHTML document) it must be designated as onclick.

Spaces, newlines, and format control characters

JavaScript ignores spaces that may be present between tokens in a program. Additionally, JavaScript also largely ignores newlines. Therefore, spaces and newlines can be used without restrictions in the source code of programs to format and give them a readable appearance.

In addition to the regular space character (\u0020), JavaScript additionally recognizes the following characters as whitespace: tab (\u0009), vertical tab (\u000B), format feed (\u000C), non-breaking space (\u00A0), byte order marker (\uFEFF ), as well as all Unicode characters classified as Zs.

The following characters are recognized as end-of-line characters by JavaScript interpreters: line feed (\u000A), carriage return (\u000D), line separator (\u2028) and paragraph separator (\u2029). The sequence of carriage return and line feed characters is interpreted as a single line termination character.

Unicode format control characters (category Cf), such as RIGHT-TO-LEFT MARK (\u200F) and LEFT-TO-RIGHT MARK (\u200E), control the visual presentation of the text in which they appear. They are of great importance for correct display text in some languages ​​and are valid in JavaScript comments, string literals, and regular expression literals, but not in identifiers (such as variable names) defined in JavaScript programs. The exceptions are ZERO WIDTH JOINER (\u200D) and ZERO WIDTH NON-JOINER (\u200C), which can be used in identifiers provided that they are not the first characters of the identifiers.

You can view the complete table of Unicode characters on the Unicode Table website.

Optional semicolons

Like other programming languages, JavaScript uses a semicolon (;) to separate statements from each other. The use of semicolons is essential to making the programmer's intent clear: without this separator, the end of one instruction can be mistaken for the beginning of the next, and vice versa.

Typically in JavaScript you don't have to put a semicolon between statements if they are on different lines. (The semicolon can also be omitted at the end of the program or if the next token in the program is a closing curly brace.) Many JavaScript programmers use semicolons to explicitly mark the ends of statements, even when they are not necessary.

Take a look at the following snippet. Since the two statements are on different lines, the first semicolon can be omitted:

However, if these instructions are written as shown below, the first semicolon becomes required:

Comments

JavaScript supports two ways of formatting comments. Any text between the // characters and the end of the line is treated as a comment and is ignored by JavaScript. Any text between the characters /* and */ is also considered a comment. These comments can span multiple lines, but cannot be nested. The following lines are valid JavaScript comments:

// This is a one-line comment. /* This is also a comment */ // and this is another comment. /* This is another comment. It is located in several lines. */

Identifiers and reserved words

An identifier is simply a name. In JavaScript, identifiers act as names of variables and functions, as well as labels for some loops. Identifiers in JavaScript must start with a letter, either an underscore (_) or a dollar sign ($) . This can be followed by any letters, numbers, underscores, or dollar signs. (A digit cannot be the first character, since this would make it difficult for the interpreter to distinguish identifiers from numbers.) Examples of valid identifiers:

I my_variable_name v13 _myvar $str

For compatibility and ease of editing, only ASCII characters and numbers are typically used to compose identifiers. However, JavaScript allows the use of letters and numbers from the full Unicode character set in identifiers. This allows programmers to name variables in their native languages ​​and use mathematical symbols in them:

Var name = "Alexander"; var Π = 3.14;

JavaScript reserves a number of identifiers that act as keywords for the language itself. These keywords cannot serve as identifiers in programs. JavaScript also reserves some keywords that are not currently part of the language, but which may become part of it in future versions. The table below lists all keywords by category:

Reserved JavaScript Keywords Category Keywords
Basic identifiers break delete function return typeof
case do if switch var
catch else in this void
continue false instanceof throw while
debugger finally new true with
default for null try
New keywords in the EcmaScript 5 standard class const enum export
extends import super
Reserved words in strict mode (they are available in normal mode) implements let private public yield
interface package protected static

JavaScript syntax is a set of rules for how JavaScript programs are built.

JavaScript Programs

A computer program is a list of "instructions" to be "executed" by the computer.

In a programming language, these program instructions are called statements.

JavaScript is a programming language.

JavaScript statements are separated by semicolons.

HTML, JavaScript programs can be executed using a web browser.

JavaScript Statements

JavaScript statements consist of:

Values, Operators, Expressions, Keywords and Comments.

JavaScript values

JavaScript syntax defines two types of values: Fixed values ​​and variable values.

Fixed values ​​are called literals. The values ​​of variables are called variables.

JavaScript literals

The most important rules for writing fixed values ​​are:

Numbers are written with or without decimal places:

Lines of text written in double or single quotes:

JavaScript Variables

In a programming language, variables are used to store data values.

JavaScript uses the var keyword to declare variables.

The equal sign is used to assign values ​​to variables.

In this example, x is defined as a variable. Then x is assigned (given) the value 6:

JavaScript Operators

JavaScript uses the assignment operator (=) to assign values ​​to variables:

JavaScript uses arithmetic operators(+ - * /) to calculate values:

JavaScript Expressions

An expression is a combination of values, variables, and operators that evaluates to a value.

The calculation is called estimation.

For example, 5 * 10 takes the value 50:

Expressions can also contain variable values:

Values ​​can be various types, such as numbers and strings.

For example, "John" + "," + "Doe" takes the value "John Doe":

JavaScript Keywords

JavaScript keywords are used to define the actions to be performed.

The var keyword tells the browser to create a new variable:

JavaScript Comments

Not all JavaScript statements will be "executed".

Code after double slashes // or between /* and */ is treated as a comment.

Comments are ignored and will not be executed:

JavaScript Identifiers

Identifiers are names.

In JavaScript, identifiers are used to name variables (and keywords, as well as functions and labels).

The rules for legal names are the same in most programming languages.

In JavaScript, the first character must be a letter, an underscore (_) or a dollar sign ($) .

Subsequent characters can be letters, numbers, underscores, or dollar signs.

Numbers are not allowed as the first character.
This way JavaScript can easily distinguish identifiers from numbers.

JavaScript is case sensitive

All JavaScript identifiers are case sensitive.

The LastName and LAST NAME variables are two different variables.

JavaScript does not interpret VAR or Var as keyword var.

JavaScript and Camel Case

Historically, programmers have used three ways to combine multiple words into a single variable name:

Hyphens:

first-name, last-name, master-card, inter-city.

Hyphens are not allowed in JavaScript. It is intended for subtractions.

Underscore:

first_name, last_name, master_card, inter_city.

Camel Case:

FirstName, LastName, MasterCard, InterCity.

In programming languages, especially JavaScript, the camel case often begins with a lowercase letter:

firstName, lastName, masterCard, interCity.

JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers (almost) every symbol, punctuation and glyph in the world.

Connection