DEV Community

Cover image for Paradigms In JavaScript
Al-amin Yusuf
Al-amin Yusuf

Posted on

Paradigms In JavaScript

Programming Paradigms In Javascript

Let's start by defining what javascript is;

JavaScript is an interpreted programming language used by the browser but can also be used at the server side (Nodejs): which is a runtime composed by a javascript engine used by chrome known as V8 and some features from C++.

Paradigms in javascript

Javascript is a multi-paradigm programming language that includes functional, object-oriented, procedural, and prototypal programming.

The most common and practical paradigms in javascript are Functional Programming and Object Oriented Programming which both when mastered, give developers the ability to write robust code in a way that may seem efficient to a specific program.

Now we take a look at each of the above-listed Paradigms;

Functional Programming

Functional programming AKA Declarative programming is a programming pattern centered around functions is involves closures and lambdas to execute some certain task. Here is an example below.

const developers = [
    name: "James",
    yearsOfExperience: 2,
    language: "javascript",
    name: "Adam",
    yearsOfExperience: 5,
    language: "javascript",
    name: "Will",
    yearsOfExperience: 1,
    language: "javascript",
    name: "Sam",
    yearsOfExperience: 7,
    language: "javascript",

function listDevelopers() {
  developers.forEach((developer) => {
    const { name, experience_year, language } = developer;
      `${name} has ${yearsOfExperience} years of experience in ${language}`
} // James has 2 years of experience in javascript
// Adam has 5 years of experience in javascript
// Will has 1 years of experience in javascript
// Sam has 7 years of experience in javascript
Enter fullscreen mode Exit fullscreen mode

How about we go further by peeking at what closures are, A closure is a function which has another function embedded inside a function with reference to its lexical scope (surrounding state) eg;

function closure() {
  const name = "Carl";
  return function greeting() {
    return "Hello" + name;
} // return [function greeting]
Enter fullscreen mode Exit fullscreen mode

This may seem gibberish to a lot of new developers, I also felt that when I was first read about it but what it basically does is it logs hello plus the given name given as argument.

Object Oriented Programming

Object-oriented programming AKA Imperative prgramming by the name implies it's a pattern of programming using objects, it consists of classes objects and prototypes eg;

const John = {
  skills: ["Python", "Javascript", "SQL"],
  age: 22,
  language: "English",
  skilled: function skilled() {
    if (this.skills.length > 2) {
      return "He's got some skills";
    } else {
      return "Fair Enough";
Enter fullscreen mode Exit fullscreen mode

The above example isn't something big but just an insight on object literals, an account on how to mutate values of an object and access it as well.

Now we try something much complex a practical

class Developer {
  constructor(takes_in_something) {
    // constructor
    this.takes_in_something = takes_in_something;

  createMethod(param) {
    // method runs when new instance gets invoked
Enter fullscreen mode Exit fullscreen mode

To make a new instance of the class;

const developer = new Developer();

await developer.createMethod(param);
Enter fullscreen mode Exit fullscreen mode

The main advantage of using this type of approach is because it eliminates redundancy in code that's why developers use helper functions for code abstraction.

Top comments (0)