const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const x = null;
const y = undefined;
const z = 0;
const result1 = x ?? 'default';
const result2 = y ?? 'default';
const result3 = z ?? 'default';
const combined = result1 ? result2 ? result3 : 'B' : 'C';
console.log(combined);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
async function processValues() {
try {
console.log('Start');
const a = await Promise.resolve('First');
console.log(a);
const b = await Promise.reject('Error');
console.log(b);
return 'Done';
} catch (err) {
console.log(err);
return 'Recovered';
} finally {
console.log('Finally');
}
}
processValues().then(result => console.log(result));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log(typeof null);
console.log(typeof undefined);
console.log(typeof {});
console.log(typeof []);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const a = [1, 2, 3];
const b = a.flatMap(x => [x, x * 2]);
console.log(b);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const json = '{"name":"Sarah","age":25,"active":true}';
const obj = JSON.parse(json);
obj.name = "Emma";
obj.age++;
const json2 = JSON.stringify(obj);
const obj2 = JSON.parse(json2);
obj.age = 100;
console.log(obj2.age);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const numbers = [1, 2, 3, 4, 5];
const pipeline = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
const multiply = (x) => (y) => x * y;
const add = (x) => (y) => x + y;
const square = (x) => x * x;
const transform = pipeline(
multiply(2),
add(3),
square
);
const result = numbers.map(transform);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const multiplier = 3;
function createCounter() {
let count = 0;
const multiplier = 5;
return function() {
count++;
return count * multiplier;
};
}
const counter = createCounter();
console.log(counter());
console.log(counter());
console.log(multiplier);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const clothes = [ 'jacket', 't-shirt' ];
clothes.length = 0;
console.log(clothes[0]);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const weakMap = new WeakMap();
const obj1 = {};
const obj2 = { key: 'value' };
weakMap.set(obj1, obj2);
const result = weakMap.get(obj1).key.split('').reverse().join('');
console.log(result);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
for (var i =0; i < 10; i++){
setTimeout(function (){
console.log(i);
}, 0);
}
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const x = 0.1 + 0.2;
const y = 0.3;
console.log(x === y);
console.log(x.toFixed(1) === y.toFixed(1));
console.log(+x.toFixed(1) === +y.toFixed(1));
const num = 42;
console.log(num.toString(2));
console.log(parseInt('101010', 2));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const x = 5;
const y = 10;
const result = `${x + y}`;
const nested = `Value: ${`${x}` + `${y}`}`;
const expr = `${x}${y}`;
console.log(result);
console.log(nested);
console.log(expr);
console.log(typeof result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = Object.seal({ a: 1, b: { c: 2 } });
obj.a = 10;
obj.b.c = 20;
obj.d = 30;
delete obj.a;
const frozen = Object.freeze({ x: 5, y: { z: 10 } });
frozen.x = 50;
frozen.y.z = 100;
delete frozen.y;
console.log(obj.a, obj.b.c, obj.d, frozen.x, frozen.y.z);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
queueMicrotask(() => console.log('4'));
setTimeout(() => console.log('5'), 0);
Promise.resolve().then(() => {
console.log('6');
return Promise.resolve();
}).then(() => console.log('7'));
console.log('8');
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const array = Array.from({ length: 5 }, () => Math.random() > 0.5);
console.log(array);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = {
name: 'Sarah',
getName() { return this.name; },
getNameArrow: () => this.name
};
const { getName, getNameArrow } = obj;
const boundGetName = obj.getName.bind(obj);
console.log(getName());
console.log(getNameArrow());
console.log(boundGetName());
console.log(obj.getName());
console.log(obj.getNameArrow());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const array = Array.from({ length: 5 }, () => Math.random() > 0.5);
console.log(array);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const memoryLeak = () => {
const cache = new Map();
const weakCache = new WeakMap();
const objKey = { id: 123 };
const data = { name: 'User data', value: 42 };
cache.set(objKey, data);
weakCache.set(objKey, data);
// Simulate removing reference to key
const result = { map: cache.has(objKey), weakMap: weakCache.has(objKey) };
// objKey = null; // This would be an error, as const can't be reassigned
return result;
};
console.log(memoryLeak());
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
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
const symbol1 = Symbol('desc1');
const symbol2 = Symbol('desc2');
const myObject = {};
myObject[symbol1] = 'Value1';
myObject[symbol2] = 'Value2';
let output = '';
for (let key in myObject) {
output += myObject[key] + ' ';
}
output += Object.getOwnPropertySymbols(myObject).length;
console.log(output);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM