01/07/2023

Alternative to Eval in JS

The Eval function is super useful but extremely dangerous to use here's a simple alternative to in.

When you think of a function you usually think of using it by its keyword like this:

function myFunction() {
    // code
}

However this isn't the only way to use it, function is also its own function Function to use it to evaluate code for you you can do something like this:

let code = `(test) => {
    console.log(test);
}`;

let value = new Function("return "+code);

This will return a function that returns your function back. To use your function you will have to first call the new Function and then use your function.

value()("Hello")
// "Hello"
Back

Comments


Be the first to comment!

Read More

Making sticky elements in HTML and CSS.

Sticky elements are a useful feature in modern web design, as they allow certain elements on a webpage to remain visible even when the user scrolls. This can be useful for elements such as the n...

Node JS Logo
Node JS Logo

Setting up Node.JS for production

Node.js is a popular JavaScript runtime that allows developers to build server-side applications with JavaScript. PM2 is a process manager for Node.js applications that helps to keep your applications running smoothly and automatically restart th...

Extracting URL Parts

JavaScript provides several built-in methods that can be used to extract different parts of a URL. Here is a tutor...