Fundamental
Variables
var x = 1; // Too old...
const y = 3;
let z = 2;
let
To create a variable, use the let keyword.
let message;
message = "Hello"; // store the string 'Hello' in the variable named message
const
To declare a constant (unchanging) variable, use const instead of let:
const myBirthday = "29.08.2000";
myBirthday = "15.02.2004"; // error, can't reassign the constant!
For the object constant variable, we still can reassign its fields, but not the whole object itself.
const obj = {
  name: "Tu",
};
obj.name = "Tuslipid"; // name: Tuslipid
obj = {
  name: "Tusss",
}; // error, can't reassign the constant!
var
In older scripts, you may also find another keyword: var instead of let:
var message = "Hello";
The var keyword is almost the same as let. It also decalares variable, but in a slightly different, old-school
 way.
There are subtle differences between let and var, learn more at The old var
.
It is recommended to use
const,letinstead ofvar.
Data types
NaN, null and undefined
NaNA computational error. It is a result of an incorrect or and undefined mathematical operation.NaNis sticky. Any further mathematical operation onNaNreturnsNaN.
alert("not a number" / 2); // NaN, such division is erroneous
alert(NaN + 1); // NaN
nullIn JavaScript,nullis not areference to a non-existing object
or anull pointer
like in some other languages. It's just a speical value shich representsnothing
,empty
orvalue unknown
.
let age = null; // age is unknown
undefinedThe meaning ofundefinedisvalue is not assigned
. If a variable is declared, but not assigned, then its value isundefined:
let age;
alert(age); // undefined
Interaction
alert
prompt
confirm
Comparisons
== vs ===
A strict equality operator === checks the equality without type conversion. In other words, if a and b are different types, then a === b immediately returns false without an attempt to convert them.
In a == b, if the type of a is different from b, b needs converting to the same type of a then check the equality.
0 == false; // true
0 === false; // false
Logical operators
! (NOT)
The boolean NOT operator accepts a single argument and does the following:
Converts the operand to boolean type:
true/false.Returns the inverse value.
true/false->false/true
// The boolean value of all variables except 0, false, null, undefined are TRUE.
!true; // false
!"non-empty string"; // false
![1, 2, 3]; // false
!!"non-empty string"; // true
!0; // true
!null; // true
!undefined; // true
!false; // true
!a does not mean
a != nullora !== null && a !== undefined.
Nullish coalescing operator ??
?? returns the first arguments if it's not null/undefined. Otherwise, the second one.
result = a ?? b;
// same as
result = a != null ? a : b;
Comparison with ||:
||returns the first truthy value.||doesn't distinguish betweenfalse,0, and empty string""andnull/undefined.??returns the first defined value.
let height = 0;
alert(height || 100); // 100
alert(height ?? 100); // 0
Loop for
for(basic)forEach
array.forEach((element) => element);
for...in
for (const key in obj) {
  if (Object.hasOwnProperties(key)) {
    const element = obj[key];
  }
}
for...of
for (const iterator of obj) {
  const value = iterator;
}
Functions
Function declaration:
function func() {}
Function expression:
const func = function () {};
Arrow function:
const func = () => {};
Code quality
Polyfills and transpilers
Polyfills
The scripts that update/add new functions.
Transpilers
Translate code to a different version.