Object Reference
Data types
Hoisting (TDZ - temporare deadzone)
Code style
Array methods
Set, WeakSet
Map, WeakMap
This
Constructor functions
First class functions
Higher order functions
Closure
Pure vs Impure functions
Currying
IIFE (immediately invoked function expression)
Naming
Class (getter, setter)
Cookie
Sinxron / asinxron
Promise / callback
If src is set, the script content is ignored.
A single
The example above can be split into two scripts to work:
Semicolons
This would also work:
alert('Hello')
alert('World')
This would also work:
alert('Hello')
alert('World')
Here, JavaScript interprets the line break as an “implicit” semicolon. This is called an “automatic semicolon insertion”.
const toggleElements = document.querySelectorAll('.toggle');
toggleElements.forEach(el => {
el.addEventListener('click', function() {
this.classList.toggle('active');
});
});
const toggleElements = document.querySelectorAll('.toggle');
toggleElements.forEach(el => {
el.addEventListener('click', () => {
this.classList.toggle('active'); // `this` refers to `Window`
// Error: Cannot read property 'toggle' of undefined
});
});
const toggleElements = document.querySelectorAll('.toggle');
toggleElements.forEach(el => {
el.addEventListener('click', (e) => {
e.currentTarget.classList.toggle('active'); // works correctly
});
});
For a long time, JavaScript evolved without compatibility issues. New features were added to the language while old functionality didn’t change.
That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript’s creators got stuck in the language forever.
This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: "use strict"
A variable is a “named storage” for data.
A BigInt value is created by appending n to the end of an integer:
// the "n" at the end means it's a BigInt
const bigInt = 1234567890123456789012345678901234567890n;
Bitwise operators treat arguments as 32-bit integer numbers and work on the level of their binary representation.
These operators are not JavaScript-specific. They are supported in most programming languages.
The list of operators:
AND ( & )
OR ( | )
XOR ( ^ )
NOT ( ~ )
LEFT SHIFT ( << )
RIGHT SHIFT ( >> )
ZERO-FILL RIGHT SHIFT ( >>> )
let height = 0;
alert(height || 100); // 100
alert(height ?? 100); // 0
// Function Declaration
function sum(a, b) {
return a + b;
}
// Function Expression
let sum = function(a, b) {
return a + b;
};
Do'stlaringiz bilan baham: |