![]() What’s the completion value of that statement? The most obvious answer is to type the statement into your browser’s developer console, because when you execute it, the console by default reports the completion value of the most recent statement it executed. How would you even go about seeing the completion value of a statement? It’s a fairly little known fact that statements all have completion values (even if that value is just undefined). This is generally referred to as an “expression statement.” Statement Completion Values The third line contains just the expression b, but it’s also a statement all by itself (though not a terribly interesting one!). The a = 3 * 6 and b = a assignments (minus the vars) are called assignment expressions. var a = 3 * 6 and var b = a are called “declaration statements” because they each declare a variable (and optionally assign a value to it). Moreover, each of the three lines is a statement containing expressions. The a and b expressions both evaluate to the values stored in those variables at that moment, which also happens to be 18. But a on the second line is also an expression, as is b on the third line. In this snippet, 3 * 6 is an expression (evaluates to the value 18). Statements are sentences, expressions are phrases, and operators are conjunctions/punctuation.Įvery expression in JS can be evaluated down to a single, specific value result. These rules are collectively called the grammar of the English language.Īnd so it goes with JavaScript grammar. Some phrases are incomplete and don’t accomplish much by themselves, while other phrases can stand on their own. ![]() A phrase can itself be made up of smaller phrases. It’s comprised of one or more “phrases,” each of which can be connected with punctuation marks or conjunction words (“and,” “or,” etc). To draw the distinction, let’s borrow from terminology you may be more familiar with: the English language.Ī “sentence” is one complete formation of words that expresses a thought. But here we need to distinguish between the two, because there are some very important differences in our JS programs. It’s fairly common for developers to assume that the term “statement” and “expression” are roughly equivalent. So our focus here in this chapter is most accurately described as grammar, even though the raw syntax of the language is what developers directly interact with. In other words, discussing syntax without grammar would leave out a lot of the important details. The grammar for JavaScript is a structured way to describe how the syntax (operators, keywords, etc.) fits together into well-formed, valid programs. There are nuanced differences, but they mostly don’t matter for our discussion here. Note: The term “grammar” may be a little less familiar to readers than the term “syntax.” In many ways, they are similar terms, describing the rules for how the language works. You may think you know how to write JS, but there’s an awful lot of nuance to various parts of the language grammar that lead to confusion and misconception, so we want to dive into those parts and clear some things up. The last major topic we want to tackle is how JavaScript’s language syntax works (aka its grammar). ![]() You-Dont-Know-JS You Don’t Know JS: Types & Grammar Chapter 5: Grammar ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |