DEV Community

Jagadeesh
Jagadeesh

Posted on • Updated on

JS Code Snippets

Array

1. Find an element in an array using find() method.

let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let number = numbers.find(_number => _number === 8);
console.log(number);    // 8

number = numbers.find(_number => _number === 80);
console.log(number);    // undefined
Enter fullscreen mode Exit fullscreen mode

2. tbd

3. tbd

4. tbd

Blocking/NonBlocking or Synchronus/Asynchronus Code

Synchronus or Blocking code

console.log('Before');
console.log('After');

// Result
// Before
// After
Enter fullscreen mode Exit fullscreen mode

Asynchronus or NonBlocking code

console.log('Before');

setTimeout(() => {
    console.log('Reading a user from DB...')
}, 2000);

console.log('After');

// Result
// Before
// After
// Reading a user from DB...
Enter fullscreen mode Exit fullscreen mode

setTimeout() schedules for future run. In this case after 2sec, there will be a console message.

It's not a multithread or parallel run. Still it's a single thread run and due to scheduling, execution is not blocked.

  1. Callback
  2. Promises
  3. Async/Await (syntactic over promise)

1. Callback

  • tbd

2. Promises

  • tbd

3. Async/Await

  • tbd

Top comments (0)