Imperative vs Declarative Paradigms

JS
S
JavaScript

Simple example of the two most common JavaScript programming paradigms.

1// JavaScript Paradigms
2const samples = [
3  {
4    type: "chemistry",
5    value: 20
6  },
7  {
8    type: "metal",
9    value: 335
10  },
11  {
12    type: "liquid",
13    value: 66558
14  }    
15]
16
17// Imperative
18function getCorrosives(samples){
19  const corrosives = samples.filter((sample) => {
20     if(sample.value > 100){
21       return sample;
22     }
23  });
24  return corrosives;
25} 
26const corrosivesArray = getCorrosives(samples);
27
28
29// Declarative (Functional programming)
30const corrosives = samples.filter(sample => sample.value > 100 );
31console.log(corrosives);

Created on 11/23/2017