async function foo() {
console.log('Start');
await Promise.resolve().then(() => {
console.log('Inside Promise');
});
console.log('End');
}
foo();
console.log('Outside');Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
const promise1 = Promise.resolve('first');
const promise2 = new Promise(resolve => {
resolve('second');
});
const promise3 = Promise.resolve().then(() => 'third');
async function test() {
console.log('start');
const result1 = await promise1;
console.log(result1);
const result2 = await promise2;
console.log(result2);
const result3 = await promise3;
console.log(result3);
console.log('end');
}
test();Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const person = {};
person.name.toUpperCase();
console.log(person.name);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const matrix = [
[2, 4],
[6, 8],
];
const result = matrix.reduceRight((acc, row) => acc.concat(row.map(num => num * 2)), []);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const firstArrayData = [ 'JavaScript', 'Universe' ];
const secondArrayData = [ 'JavaScript', 'Universe' ];
console.log(firstArrayData == secondArrayData);
console.log(firstArrayData === secondArrayData);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const sym1 = Symbol("id");
const sym2 = Symbol("id");
console.log(sym1 === sym2);Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const original = { a: 1, b: { c: 2 } };
const shallow = { ...original };
const deep = JSON.parse(JSON.stringify(original));
shallow.a = 10;
shallow.b.c = 20;
deep.a = 100;
deep.b.c = 200;
const frozen = Object.freeze({ x: 1, y: { z: 2 } });
frozen.x = 99;
frozen.y.z = 99;
console.log(original.a, original.b.c, frozen.x, frozen.y.z);Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
let funcs = [];
for (var i = 0; i < 3; i++) {
funcs.push(() => i);
}
console.log(funcs[0]());
console.log(funcs[1]());
console.log(funcs[2]());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
async function fetchData() {
console.log('1');
return Promise.resolve('data');
}
async function processData() {
console.log('2');
const result = await fetchData();
console.log('3');
return result;
}
console.log('4');
processData().then(() => console.log('5'));
console.log('6');Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const person = {
name: "John",
greet: function() {
const getMessage = () => `Hello, ${this.name}`;
return getMessage();
}
};
console.log(person.greet());Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const arr = new Array(1000000).fill(0).map((_, i) => i);
const results = [];
function method1() {
return arr.filter(x => x % 2 === 0).map(x => x * 2).slice(0, 3);
}
function method2() {
const result = [];
for (let i = 0; i < arr.length && result.length < 3; i++) {
if (arr[i] % 2 === 0) {
result.push(arr[i] * 2);
}
}
return result;
}
console.log(method1().join(','));
console.log(method2().join(','));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function* infiniteGenerator() {
let i = 0;
while (true) {
yield i++;
}
}
const weakMap = new WeakMap();
const gen = infiniteGenerator();
weakMap.set(gen, gen.next().value);
const result = weakMap.get(gen) + gen.next().value;
console.log(result);Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const words = ['a', 'b', 'c'];
const result = words.concat(1, 2, 3);
console.log(result);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function foo() {
return typeof arguments;
};
console.log(foo(1, 2, 3));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const a = { value: 1 };
const b = Object.create(a);
b.value = 2;
console.log(b.value);
console.log(a.value);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { a: 1, b: { c: 2 } };
const frozen = Object.freeze(obj);
frozen.a = 99;
frozen.b.c = 88;
frozen.d = 77;
const sealed = Object.seal({ x: 10, y: 20 });
sealed.x = 30;
sealed.z = 40;
delete sealed.y;
console.log(obj.a, obj.b.c, obj.d);
console.log(sealed.x, sealed.y, sealed.z);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class StateMachine {
constructor() {
this.state = 'idle';
this.transitions = {
idle: { start: 'running' },
running: { pause: 'paused', stop: 'idle' },
paused: { resume: 'running', stop: 'idle' }
};
}
transition(action) {
const validTransitions = this.transitions[this.state];
if (validTransitions && validTransitions[action]) {
this.state = validTransitions[action];
return true;
}
return false;
}
}
const machine = new StateMachine();
console.log(machine.transition('pause'));
console.log(machine.state);
console.log(machine.transition('start'));
console.log(machine.state);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
Россияне массово переходят на новый способ заработка – арбитраж криптовалют
Арбитраж – это единственная сфера, где можно зарабатывать от 5.000р в день. Причем потолка дохода вообще нет – те кто еще вчера не знал, что такое арбитраж, сегодня выходят на 200, 300 и даже 500 тысяч в месяц.
Чтобы войти в эту нишу, пока она не перегрелась, пользуйтесь инструкциями с канала Арбитраж Криптовалют.
Он идеально подходит людям, которые ничего не знают про заработок в интернете, но при этом хотят выйти на доход в $1000-2000.
Без вложений. Без серых схем и прочего барахла.
Успейте присоединиться, начать никогда не поздно: 👉 @arbcrypto
Арбитраж – это единственная сфера, где можно зарабатывать от 5.000р в день. Причем потолка дохода вообще нет – те кто еще вчера не знал, что такое арбитраж, сегодня выходят на 200, 300 и даже 500 тысяч в месяц.
Чтобы войти в эту нишу, пока она не перегрелась, пользуйтесь инструкциями с канала Арбитраж Криптовалют.
Он идеально подходит людям, которые ничего не знают про заработок в интернете, но при этом хотят выйти на доход в $1000-2000.
Без вложений. Без серых схем и прочего барахла.
Успейте присоединиться, начать никогда не поздно: 👉 @arbcrypto
const userInput = "<script>alert('xss')</script>";
const sanitized = userInput.replace(/<script[^>]*>.*?<\/script>/gi, '');
const users = new Map();
users.set('admin', { password: 'secret123', role: 'admin' });
users.set('guest', { password: 'guest', role: 'user' });
function authenticate(username, password) {
const user = users.get(username);
return user && user.password === password ? user.role : null;
}
const role1 = authenticate('admin', 'secret123');
const role2 = authenticate('guest', 'wrong');
const role3 = authenticate('hacker', 'secret123');
console.log(sanitized);
console.log(role1, role2, role3);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var str="abcde";
for (var i=0;i<str.length;i++){
console.log(str.charAt(i),str.charCodeAt(i));
}
Ответ:
a 97
b 98
c 99
d 100
e 101
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM