Understanding the Behavior of deno eval -p with Examples and Best Practices

Understanding the Behavior of deno eval -p with Examples and Best Practices
Photo by Thành ‎ / Unsplash

The deno eval command is a powerful tool that allows developers to quickly run JavaScript or TypeScript code directly from the command line without needing a separate file. It is particularly useful for testing small snippets of code, performing quick calculations, or debugging logic. However, there are nuances to its usage, especially when combined with the -p flag.

This article explains the behavior of deno eval -p with practical examples, common pitfalls, and additional considerations for using it effectively.

What is deno eval?

The deno eval command executes JavaScript or TypeScript code provided as a string argument. This makes it ideal for one-liners or rapid experimentation.

When paired with the -p flag, the command not only executes the code but also prints the result of the last evaluated expression to the console.

Examples and Behavior

  1. Basic Example
deno eval -p "1 + 2"

Here, the command evaluates the expression 1 + 2, and since the -p flag is used, it prints the result to the console.

Output:

3

2. Using console.log()

deno eval -p "console.log('haha')"

This introduces an interesting behavior. The console.log('haha') function outputs "haha" to the console, but it doesn't return anything—it evaluates to undefined. Because the -p flag prints the result of the last evaluated expression, both the logged output and the undefined are printed.

Output:

haha
undefined

Here’s what happens:

  • The console.log('haha') logs "haha" to the console.
  • The undefined comes from the -p flag attempting to print the result of the console.log() function.
  1. Returning a Value
deno eval -p "'hello world'"

In this case, the last evaluated expression is a string literal 'hello world'. Since this expression evaluates to the string itself, the -p flag prints it directly.

Output:

hello world

Important Points to Remember

  1. The -p Flag Prints the Last Expression
    • The -p flag ensures that the result of the last evaluated expression is printed. If the last expression evaluates to undefined, that's what gets printed.
  1. Behavior with Functions Like console.log()
    • Since console.log() always returns undefined, you’ll see undefined alongside any logged output.
  1. Complex Expressions
    • You can chain multiple expressions or write more complex code:
deno eval -p "const x = 5; const y = 10; x * y"

Output:

50
  1. TypeScript Support
  • You can run TypeScript snippets directly with deno eval. For example:
deno eval -p "type User = {name: string}; const user: User = {name: 'Alice'}; user.name"

Output:

Alice

Other Considerations

  1. Escaping Strings
  • When passing strings to deno eval, remember to escape special characters in the shell. For example:
deno eval -p "\"Hello, Deno!\""

Output:

Hello, Deno!
  1. Multi-Line Code
  • For multi-line code, wrap the entire snippet in double quotes and use semicolons appropriately:
deno eval -p "const a = 1; const b = 2; a + b"

Output:

3
  1. Error Handling
  • If your code has an error, Deno will display the error message:
deno eval -p "const a = 5; a / 0"

Output:

Infinity

Best Practices

  1. Use -p Only When Necessary
    • Use the -p flag when you explicitly want to print the result of the last expression. If your code relies heavily on console.log() for output, you might not need -p.
  2. Keep Snippets Simple
    • While deno eval supports multi-line code, it’s best to keep your snippets short and simple. For more complex scripts, create a separate .ts file.
  3. Avoid Side Effects
    • Be mindful of any side effects your code might introduce, such as modifying files or network requests.
  4. Readability
    • Write clear and readable code, even for one-liners. Proper spacing and use of variables can make your code easier to debug.

Finally

The deno eval -p command is a handy tool for developers who want to quickly test JavaScript or TypeScript code. By understanding its behavior—especially how the -p flag works—you can use it effectively for a variety of tasks. Remember, the -p flag always prints the last evaluated expression, and when using functions like console.log(), you’ll often see undefined as part of the output.

Mastering these small tools and their quirks can save time and make your development workflow smoother. So go ahead, experiment with deno eval, and see how it can simplify your daily tasks!

Support Us