Code snippet to throw an error for Null or Undefined

export const shouldNotBeNull = (container: any) => {const entries = Object.entries(container);let error = '';const getErrorMessage = (variable: string, token: string) =>`${variable} should not be ${token}`;const addError = (errorString: string) => (error += errorString);for (let i = 0; i < entries.length; i++) {let [key, value] = entries[i];if (value === null) {addError(getErrorMessage(key, 'null'));}if (value === undefined) {addError(getErrorMessage(key, 'undefined'));}}if (error.length > 0) {throw new Error(error);}};

--

--

--

I love learning

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

https://link.medium.com/x2cOgyjGmob

Should I use React Native?

API Calls & Spreadsheet Manipulation With Google Apps Script

Dynamic routing in Next.JS with S3, Cloudfront and Route53 via AWS Lambda@Edge

Build a Unit Test library with Node.js from Scratch

GraphQL at the REST-aurant

Focusing on Focus

Tutorial: JavaScript String Methods

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Jain

Akshay Jain

I love learning

More from Medium

CI/CD for Angular projects using AWS CodePipeline + Bitbucket

Fundamentals of Software Development: Essential Technologies

JavaScript, Git & GitHub for beginners.

❌You Don't Know How JavaScript Copy Variables (Shallow Copy)