Variables in JavaScript. JavaScript data types Javascript data types and operators

JavaScript was created by a programmer Brendan eich from Netscape and introduced in December 1995 under the name LiveScript. It was quickly renamed JavaScript, although the official name for JavaScript is ECMAScript. ECMAScript is developed and maintained by the International Organization ECMA (European Computer Manufacturers Association).

What is JavaScript?
1) JavaScript is a scripting language, or scripting. A script is a program code - a set of instructions that does not require preprocessing (for example, compilation) before launching. JavaScript code is interpreted by the browser engine when a web page is loaded. The browser interpreter performs line-by-line parsing, processing, and execution of the original program or query.

2) JavaScript is an object-oriented language with prototypal inheritance. It supports multiple built-in objects and also lets you create or delete your own (custom) objects. Objects can inherit properties directly from each other, forming a prototype object chain.

JavaScript on web pages

1. Connecting scripts to html-document

JavaScript scripts are embedded, i.e. their content is part of the document, and externalstored in a separate file with the .js extension. Scripts can be embedded in an html document in the following ways:

or the body of the page.



This method is usually used for large scripts or scripts that are reused across different web pages.

As an event handler.
Each html element has JavaScript events that fire at a certain moment. You need to add the required event to the html-element as an attribute, and specify the required function as the value of this attribute. The function to be called in response to an event being fired is event handler... As a result of the event triggering, the associated code will be executed. This method is mainly used for short scenarios, for example, you can set the background color to change when a button is pressed:

Inside element

2. Data types and variables in JavaScript

Computers process information - data. Data can be presented in various forms or types. Much of JavaScript's functionality is implemented using a simple set of objects and data types. The functionality associated with strings, numbers, and logic is based on string, numeric, and boolean data types. Other functionality, including regular expressions, dates, and math operations, is accomplished with the RegExp, Date, and Math objects.

Literals in JavaScript are a special class of data type, fixed values \u200b\u200bof one of three data types - string, numeric or boolean:

"this is the string" 3.14 true alert ("Hellow"); // "Hellow" is a literal var myVariable \u003d 15; // 15 is a literal

Primitive data type is an instance of a specific data type such as string, numeric, boolean, , and undefined.

2.1. Variables in JavaScript

The data processed by JavaScript is variables... Variables are named containers that store data (values) in computer memory that can change during program execution. Variables have name, a type and value.

Variable name, or identifier, can only include letters a-z, A-Z, digits 0-9 (a digit cannot be the first in a variable name), a $ symbol (can only be the first character in a variable or function name) and an underscore _, no spaces are allowed. Variable name length is not limited. It is possible, but not recommended, to write variable names in the letters of the Russian alphabet, for this they must be written in Unicode.

You cannot use JavaScript keywords as a variable name. Variable names in JavaScript are case sensitive, which means that the variable var message; and var Message; - different variables.

A variable is created (declared) using the var keyword followed by the variable name, for example, var message; ... You must declare a variable before using it.

Variable initialized value using the assignment operator \u003d, for example, var message \u003d "Hellow"; , i.e. the message variable is created and stored in it initial the value "Hellow". A variable can be declared without a value, in which case it is assigned the default value undefined. The value of a variable can change during script execution. Different variables can be declared on the same line, separating them with a comma:

Var message \u003d "Hellow", number_msg \u003d 6, time_msg \u003d 50;

2.2. Variable data types

JavaScript is an untyped language; you do not need to specify the data type for a specific variable when declaring it. The data type of a variable depends on the values \u200b\u200bit takes. The type of a variable can change in the course of performing operations with data ( dynamic casting). The types are converted automatically depending on the context in which they are used. For example, in expressions involving numeric and string values \u200b\u200bwith the + operator, JavaScript converts numeric values \u200b\u200bto strings:

Var message \u003d 10 + "days before vacation"; // will return "10 days before vacation"

You can get the data type of a variable using the typeof operator. This operator returns a string that identifies the corresponding type.

Typeof 35; // will return "number" typeof "text"; // will return "string" typeof true; // will return "boolean" typeof; // will return "object" typeof undefined; // will return "undefined" typeof null; // will return "object"

All data types in JavaScript are divided into two groups - simple data types (primitive data types) and composite data types (composite data types).

TO simple data types include string, numeric, boolean, , and underfined.

2.2.1. String type

Used to store a string of characters enclosed in double or single quotes. An empty character set, enclosed in single or double quotes, is an empty string. A quoted number is also a string.

Var money \u003d ""; // empty string, zero characters var work \u003d "test"; var day \u003d "Sunday"; var x \u003d "150";

You can include a single quote in a double-quoted string, and vice versa. The same type of quotation mark is turned off with the backslash character \\ (the so-called escape sequence):

Document.writeln ("\\" Good morning, Ivan Ivanovich! \\ "\\ N"); // will display "Good morning, Ivan Ivanovich!"

Strings can be compared and also concatenated using the concatenation operator +. Automatic coercion allows you to combine numbers and strings. Strings are constant, after a string is created, it cannot be changed, but a new string can be created by concatenating other strings.

2.2.2. Numeric type (number)

Used for numeric values. There are two types of numbers in JavaScript: integers (integer) and floating point numbers (floating-point number)... Integer values \u200b\u200bcan be positive, such as 1, 2, and negative, such as –1, –2, or zero. 1 and 1.0 are the same value. Most numbers in JavaScript are written in decimal, and octal and hexadecimal systems can also be used.

AT decimal system, the values \u200b\u200bof numeric variables are set using the Arabic numerals 1, 2, 3, 4, 5, 6, 7, 8, 9, 0.

AT octal number format is a sequence containing digits from 0 to 7 and starting with the prefix 0.

For hexadecimal format, the prefix 0x (0X) is added, followed by a sequence of numbers from 0 to 9 or letters from a (A) to f (F), corresponding to values \u200b\u200bfrom 10 to 15.

Var a \u003d 120; // integer decimal numeric value var b \u003d 012; // octal format var c \u003d 0xfff; // hexadecimal format var d \u003d 0xACFE12; // hexadecimal format

Floating point numbers are either fractional decimal numbers or exponential numbers. Exponential notation of numbers implies the following form: a number with a fractional decimal part, followed by the letter e, which can be specified in both upper and lower case, then an optional + or - sign and an integer exponent.

Var a \u003d 6.24; // real number var b \u003d 1.234E + 2; // real number, equivalent to 1.234 X 10² var c \u003d 6.1e-2; // real number, equivalent to 6.1 X 10‾²

2.2.3. Boolean type

This type has two values, true (true), false (false). Used to compare and check conditions.

Var answer \u003d confirm ("Did you like this article? \\ N Click OK. If not, click Cancel."); if (answer \u003d\u003d true) (alert ("Thanks!");)

There are also special types of simple values:
null type - this type has one null value, which is used to represent non-existent objects.

undefined type - the variable type underfined means the absence of the initial value of the variable, as well as a non-existent property of the object.

Composite data types consist of more than one value. These include objects and special types of objects - arrays and functions. Objects contain properties and methods, arrays are an indexed collection of elements, and functions are made up of a collection of instructions.

2.3. Global and local variables

Variables by scope are divided into global and local. Area of \u200b\u200bvisibility represents the part of the script within which a variable name is associated with that variable and returns its value. Variables declared inside the body of a function are called local, they can only be used in this function. Local variables are created and destroyed along with the corresponding function.

Variables declared inside an element