Coder Social home page Coder Social logo

phase-0-pac-1-the-constant-expression's Introduction

The Constant Expression

Learning Goals

  • Identify the constant expression
  • Explain how the constant expression stops evaluation

Introduction

Lets repeat our definition of expression

Definition: Expression: A combination of information, called data, and symbols indicating how to combine data, called operators.

What if we were to make an expression that had no operators? What if it only had data. For example, what if we just pass 9000 into a REPL:

<iframe height="400px" width="100%" src="https://repl.it/@MaxwellBenton2/FlatPrimaryLanservers?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

In response, we get 9000. This type of expression is known as the constant expression and it's very important, although very boring.

It's boring because it doesn't do anything except be itself. But it's important because it confirms that JavaScript knows when to stop applying operations. It tells JavaScript to stop, you have an answer.

Explain How the Constant Expression Stops Evaluation

Let's consider a simple arithmetic expression. Keep in mind we apply operators in "PEMDAS" order: parenthesis, exponents, multiplication, division, addition, subtraction.

We'll start with the expression:

Math Expression: Step 0

JavaScript's mission is to find a constant piece of data or a constant expression. Because of (), it goes there first. The (10 - 4) is clearly not a constant expression because of the - operator's presence. JavaScript makes a "tree" of the two sides of the operator (-) and then looks on each side to see whether those sides are constant expressions i.e. "plain old data."

Math Expression: Step 1

Since both 4 and 10 are plain old data, it can apply - to them and produce 6 โ€” a constant expression.

So what JavaScript now sees looks like this:

Math Expression: Step 2

Here again, JavaScript checks both sides of the operator * and, because both sides are now constant expressions, applies the * to 3 and 6 and creates a new constant expression, the answer (or "return value"):

Math Expression: Step 3

Whew! Fortunately, JavaScript does all this work of building a tree of operators and returning a value very quickly!

The constant expression is always the last expression in a complex expression. It's how JavaScript knows it has data that it can work with and that no other operations need to be applied.

Table Explanation

Another way of looking at this process might be to look at a table. We'll repeat all the same things we just showed graphically, but if a table makes more sense for you, then you'll like this one better!

This is an important strategy when learning to program: if you like thinking in code, try out the code; if you prefer diagrams, draw a diagram; if tables make more sense to you, build a table. An important part of learning to be a technologist is learning to build the tools that will help you learn.

Expression Has Operators? Operators Are we done? Next Step
3*(10-4) YES *, () NO Zoom in on new sub-expression in () because of PEMDAS
(10-4) YES - NO Evaluate sub-expressions
10 NO NONE YES Zoom in on expression 10. Constant expression! Return the value of the constant, we're done!
4 NO NONE YES Zoom in on expression 4. Constant expression! Return the value of the constant, we're done!
(10-4) YES - NO Replace ( 10 - 4 ) with application of - to 10 and 4 making 6
3*6 YES * NO Zoom out and replace the sub-expression with its value we just determined
3 NO NONE YES Zoom in on expression 3. Constant expression! Return the value of the constant, we're done!
6 NO NONE YES Zoom in on expression 6. Constant expression! Return the value of the constant, we're done!
3*6 YES * NO Apply * to 3 and 6 making 18
18 NO NONE YES Constant expression! Return the value of the constant, we're done!

Conclusion

While the constant expression might seem dull, it lets us (and JavaScript) know when expression evaluation is done and establishes a groundwork for all the following expressions. The first rule of Aristotle's logic is A is A; the constant expression provides a similar "foundation" for programming.

Resources

phase-0-pac-1-the-constant-expression's People

Contributors

lizbur10 avatar maxwellbenton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.