DEV Community

Joe Steinbring
Joe Steinbring

Posted on • Originally published at blog.jws.app on

Fun with string concatenation

Previously (on my other blog), we looked at string concatenation and I figured that this week, we could see what you could actually do with it. Back in 2014, I used a calculator to play around with PhoneGap. It might be time to play with the concept again.

In the above example, we have a value stored as “value” and as you click buttons on the calculator, it appends to the “value” string. The eventual evaluation of the string’s numerical value is done using eval(). It is admittedly a much less than perfect calculator, though. Once you hit “=”, the whole thing kind of breaks down. Let’s try again.

This example still uses addExpresion(), clear(), and the computed value of displayValue but it is less dependent on slice() and hacky cheats.

So, can we simplify this a little? Sure!

You’ll notice that in the third iteration, I removed an unnecessary condition (that was looking for the clicking of an operator button).

Have a suggestion of how to improve it further? Feel free to drop a comment, below.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs