100  JavaScript. Ѡ 
 


          JavaScript,    ,      .     ,     ,       .      ,          JavaScript.





100  JavaScript

Ѡ 



 



 ,2023



ISBN978-5-0062-0484-3

     Ridero







  .       ,  -    ,   ,  ,        ,       .



          JavaScript,    ,      .     ,     ,       .      ,          JavaScript.

 ,      ,             .         ,     ,    .            ,   ,       .




JavaScript:   





1.  JavaScript?


JavaScript   , -  ,      -.        ,   ,  ,     . JavaScript      -      -.




2.    JavaScript?


    JavaScript   let, const,  () var.

let x = 5; //   x   let

const PI = 3.14159; //   PI   const

var y = ""; //   y   var




3.  let, const var?


 var     ,      let  const;

 let    ,    ;

 const    ,   ,        .

:

let count = 10;

count = 20; //   let

const PI = 3.14159;

PI = 3; //   const,   

 ,       var, let  const  JavaScript:

 var: ,    var,     (function scope).  ,      ,    .

 let  const:      (block scope),  ,      ,     (      ,    if,  for,   ).

 ,  ,    var,     ,     ,     .     ,    let  const,      ,    .




4.    undefined JavaScript?


undefined    ,   ,       .

:

let x; //  x   undefined

console.log(x); // : undefined




5.   JavaScript?


    JavaScript  push, pop, shift, unshift, map  reduce, filter.          .

:

let arr = [1, 2, 3];

arr.push(4); //     

arr.pop(); //    

arr.map(item => item * 2); //   ,     2




6.   (closures)   JavaScript?


 (closure)   ,         ,      .            .

function outerFunction() {

let outerVariable = 'I am from the outer function';

function innerFunction() {

console.log(outerVariable); // innerFunction    outerVariable   

}

return innerFunction;

}

const inner = outerFunction();

inner(); // : "I am from the outer function"




7.   JavaScript?     JavaScript?


 JavaScript    ,     .    ,      . JavaScript   ,        ( ),       .

//   

function Animal(name) {

this.name = name;

}

//      Animal

Animal.prototype.sayHello = function () {

console.log("Hello, my name is " + this.name);

};

//    Animal

let cat = new Animal("Whiskers");

//   sayHello   cat

cat.sayHello(); //  "Hello, my name is Whiskers"




8.    (arrow functions)   ?     ?


  (arrow functions)    ,      this,    .      ,         .     , ,            this.

const regularFunction = function(a, b) {

return a + b;

};

const arrowFunction = (a, b) => a + b; //     

//      

const arrowFunctionWithBlock = (a, b) => {

const result = a + b;

return result;

};




9.   JavaScript?    JavaScript    ?


  JavaScript     ,      .  JavaScript        (callbacks),  (promises), async/await   ,     .

//   Promise  fetch    

fetch('https://api.example.com/data')

.then(response => response.json())

.then(data => console.log(data))

.catch(error => console.error('Error fetching data:', error));

//   async/await

async function fetchData() {

try {

const response = await fetch('https://api.example.com/data');

const data = await response.json();

console.log(data);

} catch (error) {

console.error('Error fetching data:', error);

}

}




10.     (error handling) JavaScript?


   (error handling)  JavaScript       .       trycatch,  ,    ,    try,       catch.

function divide(a, b) {

try {

if (b === 0) {

throw newError('Division by zero');

}

return a / b;

} catch (error) {

console.error('Error:', error.message);

}

}

console.log(divide(10, 2)); // : 5

console.log(divide(10, 0)); // : "Error: Division by zero"




11.  hoisting JavaScript?     ?


Hoisting  JavaScript   ,      ""      ,    .  ,        ,        .       ,       .

console.log(myVar); // : undefined

let myVar = 10;




12.    ?       ?


   ,    ,               ,   .                ,      .




13.   Event Loop JavaScript?      ?


Event Loop ( )   JavaScript,     .      ,        .         ,    .




14.        JavaScript?


    JavaScript     ,  ,     map, filter  reduce   .      ,     .

      :

1)    (First-Class Functions).  JavaScript     ,  ,     ,   ,    .

const greet = function (name) {

return `Hello, ${name}!`;

};

const sayHello = greet;

console.log(sayHello("John")); // Output: Hello, John!

2)   (Pure Functions).    ,     ,          .

function add(a, b) {

return a + b;

}

console.log(add(2, 3)); // Output: 5

3)  (Immutability).          .      ,       .

const numbers = [1, 2, 3];

const newNumbers = [numbers, 4]; //      

console.log(newNumbers); // Output: [1, 2, 3, 4]

4)    (Higher-Order Functions).  ,          .           .

const multiplyBy = function (factor) {

return function (number) {

return number * factor;

};

};

const double = multiplyBy(2);

console.log(double(5)); // Output: 10

5) .        .        .

function factorial(n) {

return n === 0 ? 1 : n * factorial(n  1);

}

console.log(factorial(5)); // Output: 120

6)  .  ,    ,   .   map, filter,  reduce.

const square = x => x * x;

const increment = x => x + 1;

const squareAndIncrement = compose(increment, square);

console.log(squareAndIncrement(3)); // Output: 10

7)  (Currying).         ,       .

const square = x => x * x;

const increment = x => x + 1;

const squareAndIncrement = compose(increment, square);

console.log(squareAndIncrement(3)); // Output: 10




15.  RESTful API?  HTTP      RESTfulAPI?


RESTful API (Representational State Transfer API)     -,    REST.       HTTP       .   REST    (stateless), -     .

HTTP  ( HTTP )     RESTful API:

1) GET.        .     .

2) POST.       .      .

3) PUT.       .     .

4) PATCH.  PUT,      ,     .

5) DELETE.      .

6) OPTIONS.        HTTP,     .

7) HEAD.  GET,       .         .

8) TRACE.        ,    ,      .

9) CONNECT.      ,   .

            RESTful API.




16.  Callback  JavaScript?       ?


Callback        JavaScript,            .

     , ,        ,       ,      .  ,   callback ,  ,        .

  callback     , ,    :

function fetchData(callback) {

//     

setTimeout(function() {

const data = '  ';

callback(data); //  callback      

}, 2000); // ,      2 

}

function displayData(data) {

console.log(' :', data);

}

fetchData(displayData); //   fetchData    displayData   callback 

   fetchData   ,      .   callback       ,    . displayData   callback ,           .

         ,       .




17.    map (), filter () reduce ()?     ?


  map(), filter()  reduce()            JavaScript:

 map()    ,          .      ,           .

const numbers = [1, 2, 3, 4];

const doubled = numbers.map(num => num * 2);

/ doubled: [2, 4, 6, 8]

 filter()    ,      ,     true.       ,       .

const numbers = [1, 2, 3, 4];

const evenNumbers = numbers.filter(num => num % 2 === 0);

// evenNumbers: [2, 4]

 reduce()   -    ,     .         ,   ,     .

const numbers = [1, 2, 3, 4];

const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

// sum: 10

   0     .




18.  Promise JavaScript?        ?


Promise   ,         .      ,        .




19.   async await JavaScript?      ?


  async  await    .       async,    Promise. await   async        ,  Promise  ,      Promise.         ,        Promise.

:

function fetchData() {

return newPromise(resolve => {

setTimeout(() => {

resolve('  ');

}, 2000);

});

}

async function getData() {

try {

const result = await fetchData();

console.log(result); // : '  '

} catch (error) {

console.error(':', error);

}

}

getData();

          ,        .




20.   Math  JavaScript?    ?


Math  JavaScript       :

Math.random()      0 ()  1 ( );

const randomNum = Math.random();

console.log(randomNum); //     0  1

Math.floor()        ;

const num = 4.9;

const roundedDown = Math.floor(num);

console.log(roundedDown); // : 4

Math.ceil()        :

const num = 4.1;

const roundedUp = Math.ceil(num);

console.log(roundedUp); // : 5

Math.abs()     :

const num = -10;

const absoluteValue = Math.abs(num);

console.log(absoluteValue); // : 10

Math.max()  Math.min()          :

const maxNum = Math.max(10, 5, 8);




  .


   .

   ,     (https://www.litres.ru/chitat-onlayn/?art=70129267)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


