var student = {
id: 123,
fio: 'Пупкин Василий',
info: {
'head-teacher': 42,
subjects: [{}, {}]
}
};
var student = {
id: 123,
fio: 'Пупкин Василий',
};
student.fio;
student['fio'];
// 'Пупкин Василий';
// 'Пупкин Василий';
var student = {
id: 123,
fio: 'Пупкин Василий',
};
student.fio = 'Пупкин Николай';
student['fio'] = 'Пупкин Николай';
var object = {
id: 42,
fio: 'Пупкин Василий Васильевич'
};
Object.keys(object);
// ['id', 'fio'] or ['fio', 'id']
var data = {
id: 42,
fio: 'Пупкин Василий Васильевич'
};
var clone = data;
var clone = data;
var clone = Object.assign({}, data);
function add(a, b) {
var sum = a + b;
return result;
}
function empty(rawDate) {
}
add(2 + 2);
empty();
// {day: 1, month: 9, year: 2015}
// undefined
var numbers = [1, 1, 2, 3];
var array = new Array(1000);
var array = new Array(1, 1, 2, 3);
new Array(1000);
new Array(1, 1, 2, 3);
var numbers = [1, 1, 2, 3, 5, 8];
numbers[5];
// 8
var numbers = [1, 1, 2, 3, 5, 8];
numbers.length;
// 6
var numbers = [1, 1, 2, 3, 5, 8];
var next = numbers[4] + numbers[5];
numbers.push(next);
numbers;
// [1, 1, 2, 3, 5, 8, 13]
var part1 = [1, 1, 2, 3];
var part2 = [5, 8];
var part3 = [13, 21];
var result = part1
.concat(part2, part3);
result;
// [1, 1, 2, 3, 5, 8, 13, 21]
var queue = [];
queue.unshift(1);
queue.unshift(2);
queue.unshift(3);
queue;
[3, 2, 1]
queue.shift(); //3
queue.shift(); //2
queue.shift(); //1
var stack = [];
stack.push(1);
stack.push(2);
stack.push(3);
stack;
[1, 2, 3]
stack.pop(); //3
stack.pop(); //2
stack.pop(); //1
var meaningOfLife = null;
var storage = ...;
function findUser(id) {
var result = storage.get(id);
if (result) {
return result;
}
return null;
}
var newVariable;
newVariable;
// undefined
function calculateNext(numbers) {
var l = numbers.length;
var a = numbers[l - 2];
var b = numbers[l - 1];
numbers.push(a + b);
}
var numbers = [1, 1, 2, 3, 5];
calculateNext(numbers);
// undefined
function add(a, b) {
console.log(a);
console.log(b);
}
add();
// undefined
// undefined
function canShowContent(user) {
if (user.age >= 18) {
return true;
}
return false;
}
function canShowContent(user) {
return user.age >= 18;
}
1 / 0;
// ZeroDivisionError: integer division
// or modulo by zero
var result = 1 / 0;
result; // Infinity
var result = -1 / 0;
result; // -Infinity
var result = 0 / 0;
result; // NaN
var result = 0 / 0;
result; // NaN
if (result === NaN) {
result = 0;
}
if (isNaN(result)) {
result = 0;
}
isFinite(NaN); // false
isFinite(Infinity); // false
isFinite(-Infinity); // false
isFinite(42); // true
isFinite(0); // true
isFinite(5e10); // true
var sum = 0.1 + 0.2;
sum === 0.3;
// false
sum; //0.30000000000000004
var question = 'УрФУ';
question.length;
// 4
var question = 'УрФУ';
question[1];
// 'р'
var question = 'екатеринбург';
question[0] = 'Е';
question;
// 'екатеринбург'; ?!
var name = 'Alex';
var surname = 'Mangin';
name + ' ' + surname;
// 'Alex Mangin'
var name = 'Alex';
var surname = 'Mangin';
name.concat(' ', surname);
// 'Alex Mangin'
var queue = '0000111111000';
var index = queue.indexOf('1');
var lastIndex = queue
.lastIndexOf('1');
lastIndex - index + 1;
// 6
var str = 'Число: 42';
var rawNumber = str.slice(7);
// 42
var str = 'Число: 42 и точка';
var rawNumber = str.slice(7, 9);
rawNumber;
// 42
var str = 'Число: 42 и точка';
var rawNumber = str.substr(7, 2);
rawNumber;
// 42
var message = 'Без учета НДС 18%';
message.toLocaleLowerCase();
// без учета ндс 18%
var message = 'купить';
message.toLocaleUpperCase();
// КУПИТЬ
var rawUserAge = ' 18 ';
rawUserAge.trim();
// '18'
var str = '13 42 4';
var rawNumbers = str.split(' ');
// ['13', '42', '4']
var some;
// что-то присваиваем
typeof some;
typeof 0;
typeof undefined;
typeof true;
typeof 'foo';
typeof {};
typeof null;
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined;
typeof true;
typeof 'foo';
typeof {};
typeof null;
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true;
typeof 'foo';
typeof {};
typeof null;
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true; // 'boolean'
typeof 'foo';
typeof {};
typeof null;
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true; // 'boolean'
typeof 'foo'; // 'string'
typeof {};
typeof null;
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true; // 'boolean'
typeof 'foo'; // 'string'
typeof {}; // 'object'
typeof null;
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true; // 'boolean'
typeof 'foo'; // 'string'
typeof {}; // 'object'
typeof null; // 'object'
typeof function(){};
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true; // 'boolean'
typeof 'foo'; // 'string'
typeof {}; // 'object'
typeof null; // 'object'
typeof function(){}; // 'function'
typeof [];
typeof 0; // 'number'
typeof undefined; // 'undefined'
typeof true; // 'boolean'
typeof 'foo'; // 'string'
typeof {}; // 'object'
typeof null; // 'object'
typeof function(){}; // 'function'
typeof []; // 'object'
var undef = 'undefined';
if (typeof localStorage !== undef) {
// ура!
}
add(2, 5); // 7
add({a: 2, b: 3}); // 5
function add(a, b) {
if (typeof a === 'object') {
b = a.b;
a = a.a;
}
return a + b;
}
var price = 100500;
var isFree = Boolean(price);
var isFree = !!price;
var isFree = !!price;
var amount = '9';
var totalAmount = 0;
amount = Number(amount);
totalAmount = totalAmount + amount;
var amount = '9';
var totalAmount = 0;
amount = +amount;
totalAmount = totalAmount + amount;
var amount = '9';
var totalAmount = 0;
amount = +amount;
totalAmount = totalAmount + amount;
var amount = '9 штук.';
var totalAmount = 0;
amount = parseInt(amount, 10);
totalAmount = totalAmount + amount;
var percent = '9.5 землекопа.';
percent = parseFloat(amount);
var price = 100500;
var message = 'Цена: ' + price;
var message = 'Цена: ' +
String(price);
var a = 2;
var b = 3;
a + b; // 5
var a = +2;
var b = '3';
a + +b; // 5
var a = 'some' + 'other';
var a = 2 + 'other';
function add(a, b) {
if (typeof(a) === 'string' ||
typeof(b) === 'string') {
return concat(toString(a),
toString(b));
} else {
return sum(toNumber(a),
toNumber(b));
}
}
if (a > 0 || b < 0 && c === '42') {
// сделай что-нибудь
}
a || b;
false || 42;
// 42;
function someFunction(a) {
a = a || 42;
}
a && b;
true || 42;
// 42;
if (object.someMethod) {
object.someMethod();
}
condition && someFunction();