Академический Документы
Профессиональный Документы
Культура Документы
Hello, World!
Script Tag
Importance of semicolon
"use strict"
1) The "use strict" directive switches the engine to the
“modern” mode, changing the behavior of some built-in
features
2) Strict mode is enabled by placing "use strict" at the top of
a script or function.
3) Strict mode is supported by all modern browsers.
4) We recommended always starting scripts with "use strict".
"use strict"
(function() {
"use strict"; 'use strict';
// this code works the // ...your code...
modern way
... })()
variables
let user = 'John'; const RED = "#F00";
let age = 25; const GREEN = "#0F0";
const BLUE = "#00F";
let message = 'Hello'; const ORANGE = "#FF7F00";
typeof 0 // "number"
undefined NaN
null 0
alert("Hello");
result = confirm(question);
While loop
let i = 0;
while (i < 3) {
alert( i );
i++;
}
for loop
do {
alert( i );
i++;
} while (i < 3);
Break and Continue in loops
let sum = 0;
switch (a) {
case 3:
alert( 'Too small' );
break;
case 4:
alert( 'Exactly!' );
break;
default:
alert( "I don't know such values" );
}
Functions
function showMessage() {
let message = "Hello, I'm JavaScript!"; // local variable
alert( message );
}
let user = {
name: "John",
age: 30
};
property of an Object
delete user.age;
let n = 1.23456;
alert( n.toFixed(2) ); // 1.23
Data Types: Number
let billion = 1000000000;
let billion = 1e9; // 1 billion, literally: 1 and 9
zeroes
let ms = 0.000001;
let ms = 1e-6; // six zeroes to the left from 1
Data Types: Number
alert( 0xff ); // 255
alert( 0xFF ); // 255 (the same, case doesn't matter)
alert( 123456..toString(36) );
Data Types: Number
Math.floor
Rounds down: 3.1 becomes 3, and -1.1 becomes
-2.
Math.ceil
Rounds up: 3.1 becomes 4, and -1.1 becomes -1.
Data Types: Number
Math.round
Rounds to the nearest integer: 3.1 becomes 3,
3.6 becomes 4 and -1.1 becomes -1.
alert( `My\n`.length ); // 3
str[0] = 'h';
alert( str[0] ); // doesn't work
Data Types: String
let str = 'Hi';
alert( str ); // hi
str.lastIndexOf(substr, position)
Getting a substring
For instance:
// mix of values
let arr = [ 'Apple', { name: 'John' }, true, function() { alert('hello'); } ];
fruits.push("Pear");
fruits.unshift('Apple');
fruits.push("Orange", "Peach");
fruits.unshift("Pineapple", "Lemon");
let arr = fruits; // copy by reference (two variables reference the same array)
// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice(2, 0, "complex", "language");
arr.concat(arg1, arg2...)
Data Types: Array
let arr = [1, 2];
let arrayLike = {
0: "something",
length: 1
};
The methods arr.indexOf, arr.lastIndexOf and arr.includes have the same syntax and do
essentially the same as their string counterparts, but operate on items instead of characters:
arr.indexOf(item, from) looks for item starting from index from, and returns the index
where it was found, otherwise -1.
arr.lastIndexOf(item, from) – same, but looks from right to left.
arr.includes(item, from) – looks for item starting from index from, returns true if found.
Data Types: Array
find and findIndex
Imagine we have an array of objects. How do we find an object with the specific condition?
alert(user.name); // John
Data Types: Array
filter
The find method looks for a single (first) element that makes the function return
true.
If there may be many, we can use arr.filter(fn).
The syntax is similar to find, but filter continues to iterate for all array elements
even if true is already returned:
alert(someUsers.length); // 2
Data Types: Array
map
The arr.map method is one of the most useful and often used.
The syntax is:
It calls the function for each element of the array and returns the array of results.
For instance, here we transform each element into its length:
For instance:
let arr = [ 1, 2, 15 ];
Literally, all elements are converted to strings and then compared. So, the lexicographic ordering is applied
and indeed "2" > "15".
To use our own sorting order, we need to supply a function of two arguments as the argument of arr.sort().
The function should work like this:
function compare(a, b) {
if (a > b) return 1;
if (a == b) return 0;
if (a < b) return -1;
}
Data Types: Array
function compareNumeric(a, b) {
if (a > b) return 1;
if (a == b) return 0;
if (a < b) return -1;
}
let arr = [ 1, 2, 15 ];
arr.sort(compareNumeric);
alert(arr); // 1, 2, 15
Data Types: Array
reverse
For instance:
The call to split(s) with an empty s would split the string into
an array of letters:
For instance:
For instance: