DEV Community

Shavon Harris
Shavon Harris

Posted on

To Be Or Not To Be

I'm excited to get to my third day of blogging! Covering beginner concepts in Javascript is definitely bolstering my confidence, I hope its helping you as well!

Today we dive into closures once again, it's an important thing to master, but we are also looking at error handling and comparing values.

Today's Journey

In this problem we are writing a function expect that takes in a value val and returns an object that holds two functions.

  • @param {string} val
  • @return {object}
const expect = function(val){
    return {
        toBe: function (value) {},
        notToBe: function (value) {}
    };
};
Enter fullscreen mode Exit fullscreen mode

More on Closures

This function looks differently than the ones I wrote out yesterday and the day before
[https://dev.to/shavonharrisdev/encapsulation-closure-and-a-higher-order-function-walk-into-a-hackathon-403b],
https://dev.to/shavonharrisdev/closures-closures-closures-3o3n
but this is still a closure.

Why? Well because the key characteristic of a closure is its ability to remember and access variables from its parent function or lexical scope in fancy programmer jargon, even when the function is executed outside that scope. Functions toBe and notToBe access the val parameter of their parent function even after expect has finished execution.

Comparing Values

The toBe function accepts a value and returns true if the two values === each other. If they are not equal, it should throw an error "Not Equal".

In other words we will have two values that we are comparing. A value comes from the expect function (val) and another comes from the toBe function(value). We ask ourselves, or rather the program, are val and value strictly equal to each other? Are they the same value and the same data type? If yes, wonderful. If no, throw an error!

I'll also add the reverse logic in a notToBe function.

const expect = function (val) {
  return {
toBe: function (value) {
      if (val === value) {
        return true;
      } else {
        throw new Error("Not Equal");
      }
    },
notToBe: function (value) { 
      if (val !== value) {
        return true;
      } else {
        throw new Error("Equal");
      }
    }
  };
};

Enter fullscreen mode Exit fullscreen mode

What is a throw new Error 😳

throw new Error is like a special phrase that means hold up, this makes no sense!

  1. The throw is just like raising your hand and saying STOP. It stops the normal flow of your program.

  2. new Error creates a new "Error" object. If that sounds confusing just think of it as a bucket called Error that holds a note and that note explains the problem.

  3. If you say (throw new Error("message")); This is like stopping the story while someone is telling it and holding up a note that says "THAT DOESN'T MAKE SENSE AND THIS IS WHY". It's important to use clear descriptive error messages for easy understanding.

Throw new Error is really helpful for handling errors and debugging.

Try...Catch

The last thing I do is use a try catch block to log the results.

console.log("toBe", expect(5).toBe(5)); // 
try {
  console.log("notToBe", expect(5).notToBe(5)); 
} catch (e) {
  console.error(e.message); 
}
Enter fullscreen mode Exit fullscreen mode

Why use Try/ catch and not just a console.log or return?

With a throw new Error message you're basically saying if this code goes goes wrong stop everything and throw an error. However, you might not want to stop everything in a large application, I mean, one monkey doesn't need to stop the whole show 🤭. So, you wrap the code that might throw an error in a try...catch block so that you're prepared for the possibility of an error. If an error does occur you can handle it neatly, like perhaps deciding whether to just log the error, continue, or do something else. This way your program will not just stop.

const expect = function (val) {
  return {
toBe: function (value) {
      if (val === value) {
        return true;
      } else {
        throw new Error("Not Equal");
      }
    },
notToBe: function (value) { 
      if (val !== value) {
        return true;
      } else {
        throw new Error("Equal");
      }
    }
  };
};

console.log("toBe", expect(5).toBe(5)); // 
try {
  console.log("notToBe", expect(5).notToBe(5)); 
} catch (e) {
  console.error(e.message); 
}
Enter fullscreen mode Exit fullscreen mode

That's all for today, Happy Coding!

Top comments (0)