DEV Community

Jagadeesh
Jagadeesh

Posted on • Edited 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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more