var
or let
keyword. If we are talking about a constant, the keyword const
. It is possible to declare a variable and assign it some value without using these keywords, but this is not recommended.var
keyword was the only way to declare variables. var a = 0
var
in this construct, then the value will be assigned to an undeclared variable. The result of this operation depends on the mode in which the program runs. function notVar() { bNotVar = 1 // } notVar() console.log(bNotVar)
1
, usually no one expects such behavior from the program, the expression bNotVar = 1
does not look like an attempt to declare and initialize a variable, but as an attempt to access a variable that is external to the function (this is quite normal). As a result, the implicit declaration of variables confuses those who read the code and can lead to unexpected program behavior. Later we will talk about functions and areas of visibility, but in the meantime try to always, when the meaning of a certain expression is to declare a variable, to use specialized keywords. If in this example the function body is rewritten as var bNotVar = 1
, then an attempt to launch the above code snippet will result in an error message (you can see it in the browser console).Uncaught ReferenceError: bNotVar is not defined
. Its meaning is that the program cannot work with a non-existent variable. It is much better, when you first start the program, to see such an error message than to write incomprehensible code that is able to behave unexpectedly.undefined
. var a //typeof a === 'undefined'
var
keyword can be repeatedly declared again by assigning new values to them (but this can confuse the person who is reading the code). var a = 1 var a = 2
var a = 1, b = 2
var
keyword outside of a function is assigned to a global object. It has a global scope and is accessible from anywhere in the program. If a variable is declared using the var
keyword inside a function, then it is visible only inside this function, being a local variable for it.var
, a variable is declared, the name of which coincides with the name of a variable from the global scope, it will “overlap” the global variable. That is, when accessing such a variable inside a function, its local variant will be used.var
keyword has a so-called functional scope, not a block scope.var
at the end of the function code, it can also be accessed at the beginning of the code, since the JavaScript variable lifting mechanism (hoisting) works. This mechanism "raises" the declaration of variables, but not the operation of their initialization. This can be a source of confusion, so make it a rule to declare variables at the beginning of a function.var
. The scope of variables declared using the let
keyword is limited to the block, statement, or expression in which it is declared, as well as nested blocks.let color = 'red'
can be translated into English like this: “let the color be red”, and into Russian like this: “let the color be red”.let
keyword, you can get rid of the ambiguities that accompany the var
keyword (for example, you won’t be able to declare the same variable twice using let
). Using let
outside of a function, say, when initializing loops, does not create global variables. for (let i = 0; i < 5; i++) { console.log(i) } console.log(i)
i
will be declared using the var
keyword, then i
will be available outside the cycle, after it finishes its work.var
and use only the keywords let
and const
.var
or let
keywords can be overwritten. If instead of these keywords const
used, then a new value cannot be assigned to a constant declared and initialized with its help. const a = 'test'
a
cannot assign a new value. But it should be noted that if a
is not a primitive value, like a number, but an object, using the keyword const
does not protect this object from changes.const
keyword does not make objects immutable. It simply protects against changes references to them, written in the corresponding constants. Here's what it looks like: const obj = {} console.log(obj.a) obj.a = 1 // console.log(obj.a) //obj = 5 //
obj
constant, at initialization, a new empty object is written. Attempting to access its a property, a non-existent, does not cause an error. The console gets undefined
. After that we add a new property to the object and try to access it again. This time, the value of this property is set to the console - 1
. If you uncomment the last line of the example, an attempt to execute this code will result in an error.const
keyword is very similar to let
, in particular, it has a block scope.const
, resorting to let
only in special cases. Why? The thing is that it is best to strive to use the simplest available designs as possible in order not to complicate the program and avoid mistakes.typeof
operator. It returns a string indicating the type of the operand.number
string
(string)boolean
(boolean)null
(special value null
)undefined
(special value undefined
)symbol
(symbol, used in special cases, appeared in ES6)typeof
operator returns them.number
in JavaScript are represented as 64-bit double-precision floating-point numbers.0x
- it is perceived as a number written in hexadecimal notation. Numbers can be written in the exponential representation (in such numbers you can find the letter e
). 10 5354576767321 0xCC //
3.14 .1234 5.2e4 //5.2 * 10^4
Number
.a
, in which a numeric literal is written, as an object, in the Google Chrome console.toString()
method of an object of type Number
, it will return a string representation of the number. The corresponding command that can be executed in the browser console (and in the usual code) looks like this: a.toString()
Number
object can be used as a constructor, creating new numbers with it (although it is almost never used in this form), it can also be used as an independent entity without creating its instances (that is, certain numbers represented from its using). For example, its Number.MAX_VALUE
property contains the maximum numeric value that can be represented in JavaScript.string
are sequences of characters. Such values are specified as string literals enclosed in single or double quotes. 'A string' "Another string"
"A \ string"
\n
means a newline character. The backslash character can also be used to add quotes to strings enclosed in the same quotes. Escaping the quotation character with \
causes the system not to perceive it as a special character. 'I\'ma developer'
+
operator. "A " + "string"
`
) and have some interesting properties. `a string`
`a string with ${something}` `a string with ${something+somethingElse}` `a string with ${obj.something()}`
`a string with ${something}`
true
(true) and false
(false). Comparison operations, such as ==
, ===
, <
, >
, return true
or false
.if
and while
, helping to control the program flow.true
or false
is expected, other values can be used that are automatically regarded by the language as true (truthy) or false (falsy). 0 -0 NaN undefined null '' //
null
value that indicates the absence of a value. Similar values are used in other languages.undefined
written to a variable indicates that the variable is not initialized and has no value for it.return
. If the function accepts a certain parameter that, when called, is not specified, it is also set to undefined
.undefined
, you can use the following construction. typeof variable === 'undefined'
object
type, and they, although in many ways different from each other, have much in common. 1 / 2 i++ i -= 2 i * 2
'A ' + 'string' 'A ' += 'string'
2 0.02 'something' true false this // , undefined i // i
function class function* // yield // / yield* // async function* // await // /pattern/i // () //
[] // {} // [1,2,3] {a: 1, b: 2} {a: {b: 1}}
a && b a || b !a
object.property // ( ) object[property] object['property']
new object() new a(1) new MyRectangle('name', 2, {a: 4})
function() {} function(a, b) { return a * b } (a, b) => a * b a => a * 2 () => { return 2 }
ax(2) window.resize()
class
keyword.__proto__
), which points to another object that is its prototype. The object inherits the properties and methods of the prototype. const car = {}
Object
constructor. const car = new Object()
car
object is the Object.prototype
.Array.prototype
object. const list = [] // const list = new Array()
car.__proto__ == Object.prototype //true car.__proto__ == new Object().__proto__ //true list.__proto__ == Object.prototype //false list.__proto__ == Array.prototype //true list.__proto__ == new Array().__proto__ //true
__proto__
property, it does not have to be available to the developer, but you can usually access it. It should be noted that a more reliable way to get a prototype of an object is to use the getPrototypeOf()
method of the global Object
. Object.getPrototypeOf(new Object())
Object.prototype
. Array.prototype.__proto__ == Object.prototype
Object.prototype
no prototype.new
operator and applying object literals or array literals, you can create an instance of an object using the Object.create()
method. The first argument passed to this method is the object that will become the prototype of the object created with it. const car = Object.create(Object.prototype)
isPrototypeOf()
method. const list = [] Array.prototype.isPrototypeOf(list)
new
keyword is used). . . function Person(name) { this.name = name } Person.prototype.hello = function() { console.log(this.name) } let person = new Person('Flavio') person.hello() console.log(Person.prototype.isPrototypeOf(person))
this
. name
, . . - , name
, .name
, . , , , hello()
. , Person
hello()
( ). class Person { constructor(name) { this.name = name } hello() { return 'Hello, I am ' + this.name + '.' } }
new ClassIdentifier()
.constructor
, .hello()
— , , . Person
. const flavio = new Person('Flavio') flavio.hello()
class Programmer extends Person { hello() { return super.hello() + ' I am a programmer.' } } const flavio = new Programmer('Flavio') flavio.hello()
hello()
Hello, I am Flavio. I am a programmer
.super
.static
) , .get
set
. — , , . -, — . class Person { constructor(name) { this.userName = name } set name(value) { this.userName = value } get name() { return this.userName } }
Source: https://habr.com/ru/post/429838/
All Articles