JavaScript and Logic.
Tags: javascript, programming, logic, devjournal
Published: 2022-05-03 10:46:33

In **logic**, a statement (or a Proposition) is a meaningful declarative sentence that is either True (T) or False (F). Examples of logical statements include the following:

"You are reading this on dev.to" `//True` 

"You reading this on Facebook" `//False`

In JavaScript:

The author of the "_You don't know JS yet_" series, **Kyle Simpson** tells us that

"_In JavaScript, a statement might look as follows: `a  =  b  *  2;`
 The characters  a  and  b  are called  variables which are like simple boxes you can store any of your stuff in. In programs, variables hold values to be used by the program. Think of them as symbolic placeholders for the values themselves. By contrast, the  `2`  is just a value itself, called a  'literal value', because it stands alone without being stored in a variable. The  `=`  and  `*`  characters are  operators— they perform actions with the values and variables such as assignment and mathematic multiplication. Most statements in JavaScript conclude with a semicolon (;) at the end. The statement  `a = b * 2;`  tells the computer, roughly, to get the current value stored in the variable  b, multiply that value by  2, then store the result back into another variable we call  a._

_Statements are made up of one or more  expressions. An expression is any reference to a variable or value, or a set of variable(s) and value(s) combined with operators. For example: `a =  b  *  2;` This statement has four expressions in it: • `2` is a  literal value expression. • `b`  is a  variable expression, which means to retrieve its current value. • `b * 2`  is an  arithmetic expression, which means to do the multiplication. `a = b * 2`  is an  assignment expression, which means to assign the result of the  `b * 2`  expression to the variable  a  (more on assignments later). A general expression that stands alone is also called an  expression statement, such as the following: `b  *  2;` This flavor of expression statement is not very common or useful, as generally it wouldn’t have any effect on the running of the program —it would retrieve the value of  b  and multiply it by  2, but then wouldn’t do anything with that result._"




Whereas author of the "_Eloquent JavaScript_", **Marijn Haverbeke** tells us :
"_A fragment of code that produces a value is called an expression. Every value that is written literally (such as 22 or "psychoanalysis") is an expression. An expression between parentheses is also an expression, as is a binary operator applied to two expressions or a unary operator applied to one. This shows part of the beauty of a language-based interface. Expressions can contain other expressions in a way similar to how subsentences in human languages are nested—a subsentence can contain its own subsentences, and so on. This allows us to build expressions that describe arbitrarily complex computations. If an expression corresponds to a sentence fragment, a JavaScript statement corresponds to a full sentence. A program is a list of statements. The simplest kind of statement is an expression with a semicolon after it. This is a program: 
`1;` `!false;` It is a useless program, though._

 _An expression can be content to just produce a value, which can then be used by the enclosing code. A statement stands on its own, so it amounts to something only if it affects the world. It could display something on the screen—that counts as changing the world—or it could change the internal state of the machine in a way that will affect the statements that come after it. These changes are called **side effects**. The statements in the previous example just produce the values 1 and true and then immediately throw them away. This leaves no impression on the world at all. When you run this program, nothing observable happens._"

They both agree that:

(Statements > Expressions)

&&

Statements are statements because they end with a semi-colon, definitely defying Logic, but looking at it deeply, you see that it definitely in a way still has logic, because `1;` as a statement doesn't make "sense" but according to logic, it has to equate to true or false, JS does this by converting it to boolean in different cases to fulfill this part of logic (truthy, falsy comes in)


However JS does its best to fulfill this logic, there are cases where JS fails to do this and throws up an error.
E.g, `b * 2;`
This program throws an error when we try to log it on to the console that it "can't fulfill the logic of evaluating to true or false" as it fails to find the value of b variable expression assuming it was never set.

Looking at this, we see that JS and many other languages are still very much less sophisticated than the human brain in terms of reason as it requires a condition from programmer to make it reason and to make it reason requires lots of conditions underneath, the more reason it gains, the more conditions. 

That's it. **Reason**. Not speed, durability, reliability, but reason.