[{"id":"/sicpjs/1.1.1","tag":"TITLE","body":"1.1.1  Expressions"},{"tag":"TEXT","id":"#p1","child":[{"body":"\n    One easy way to get started at programming is to examine some typical\n    interactions with an interpreter for the\n    JavaScript language.\n    You type \n    a ","tag":"#text"},{"tag":"EM","child":[{"body":"statement","tag":"#text"}]},{"body":",\n    and the interpreter responds by displaying the\n    result of its ","tag":"#text"},{"tag":"EM","child":[{"body":"evaluating","tag":"#text"}]},{"body":" that\n    \n\tstatement.\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p2","child":[{"body":"\n\t  In this edition, the mouse click on \n\t  JavaScript statements with dark background is \n\t  programmed in such a way that a JavaScript interpreter is\n\t  displayed, which can ","tag":"#text"},{"tag":"EM","child":[{"body":"evaluate","tag":"#text"}]},{"body":" the\n\t  statement and display the resulting value.\n\t  By the way, the program that makes the mouse click\n\t  on a JavaScript statement display the interpreter is itself\n\t  written in JavaScript; it is called the ","tag":"#text"},{"tag":"EM","child":[{"body":"script","tag":"#text"}]},{"body":" for\n\t  the mouse click. Such scripts were a central objective in\n\t  the original design of JavaScript.\n\t","tag":"#text"}]},{"tag":"TEXT","id":"#p3","child":[{"body":"\n        One kind of statement you might type is an \n        expression statement, which consists of an\n        ","tag":"#text"},{"tag":"EM","child":[{"body":"expression","tag":"#text"}]},{"body":" followed by a semicolon.\n        \n\tOne kind of primitive expression is a number.\n      \n    (More precisely, the expression that you type consists of the numerals that\n    represent the number in base 10.)\n    \n      If you ask our script to evaluate the expression statement\n      ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":1,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwFgBwBsA3EA","body":"486; "},{"body":"\n      by clicking it, it will respond by displaying a JavaScript interpreter\n      with the option\n      to evaluate the statement by pressing a \"","tag":"#text"},{"body":"Run","tag":"#text"},{"body":"\" button.\n      Click on the primitive expression statement, and see what happens!\n    ","tag":"#text"}]},{"tag":"TEXT","id":"#p4","child":[{"body":"\n        Expressions representing numbers may be combined with\n\toperators\n        (such \n        \n        as ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"+"},{"body":"\n        or ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"*"},{"body":") to form a\n        \n        compound expression that represents the\n        application of a corresponding primitive\n        function to those numbers.  For example,\n\t\n\t  evaluate\n          any of the following expression statements\n\t  by clicking on it:\n\t","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":2,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCygzAOygDUoeALAJwDcQA","body":"137 + 349; ","output":"486"},{"tag":"SNIPPET","latex":false,"id":3,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCygDAaALSgDMpALANxA","body":"1000 - 334; ","output":"666"},{"tag":"SNIPPET","latex":false,"id":4,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwVlAFSgCcxA3EA","body":"5 * 99; ","output":"495"},{"tag":"SNIPPET","latex":false,"id":5,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCygDKGACwDcQA","body":"10 / 4; ","output":"2.5"},{"tag":"SNIPPET","latex":false,"id":6,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwJjQOygDUqADANxA","body":"2.7 + 10; ","output":"12.7"}]},{"tag":"TEXT","id":"#p5","child":[{"body":"\n        Expressions such as these, which contain other expressions\n        as components, are called ","tag":"#text"},{"tag":"EM","child":[{"body":"combinations","tag":"#text"}]},{"body":".\n        \n        Combinations that are formed by an \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"operator","tag":"#text"}]},{"body":" symbol in the middle, and\n        ","tag":"#text"},{"tag":"EM","child":[{"body":"operand","tag":"#text"}]},{"body":" expressions to the left and right of it, \n        are called \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"operator combinations","tag":"#text"}]},{"body":".\n\t\n        The value of an operator combination is\n        obtained by applying the function specified by the operator to the\n        arguments that are the values of the operands.\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p6","child":[{"body":"\n        The convention of placing the operator between the operands is\n        known as \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"infix notation","tag":"#text"}]},{"body":". It follows the mathematical notation that\n\tyou are most likely familiar with from school and everyday life.\n        As in mathematics, operator combinations can be ","tag":"#text"},{"tag":"EM","child":[{"body":"nested","tag":"#text"}]},{"body":", that\n\tis, they can have operands that\n        \n\tthemselves are operator combinations:\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":7,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwCgMygBUoArAJSgDUouKADKALSgBs5A3EA","body":"(3 * 5) + (10 - 6); ","output":"19"},{"body":"\n        As usual,\n\t\n\tparentheses are used to group operator combinations in order\n\tto avoid ambiguities. JavaScript also follows the usual conventions\n\twhen parentheses are omitted: multiplication and division bind more\n\tstrongly than addition and subtraction. For example,\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":8,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwZlAFSgCsoA1KgAyhgBMA3EA","body":"3 * 5 + 10 / 2; "},{"body":"\n        stands for \n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":9,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwCgMygBUoArAJSgDUouKADKGAEzkDcQA","body":"(3 * 5) + (10 / 2); "},{"body":"\n        We say that ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"*"},{"body":" and\n\t","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"/"},{"body":" have\n        ","tag":"#text"},{"tag":"EM","child":[{"body":"higher precedence","tag":"#text"}]},{"body":"\n\tthan ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"+"},{"body":" and\n\t","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"-"},{"body":". Sequences of additions and\n\tsubtractions are read from left to right, as are sequences of\n\tmultiplications and divisions. Thus,\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":10,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCy0BaUAVlDACZQAqUAFlAGpQBmAbiA","body":"1 - 5 / 2 * 4 + 3; "},{"body":"\n        stands for \n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":11,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwChaALSi64CsoYATAJSgBUoALDXQNSgDMA3EA","body":"(1 - ((5 / 2) * 4)) + 3; "},{"body":"\n        We say that the operators\n        ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"+"},{"body":",\n        ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"-"},{"body":",\n        ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"*"},{"body":" and\n        ","tag":"#text"},{"tag":"JAVASCRIPTINLINE","body":"/"},{"body":" are \n\t","tag":"#text"},{"tag":"EM","child":[{"body":"left-associative","tag":"#text"}]},{"body":".\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p7","child":[{"body":"\n        There is no limit (in principle) to the depth of such nesting and to the\n\toverall complexity of the expressions that the JavaScript interpreter\n\tcan evaluate. It is we humans who might get confused by still relatively\n        simple expressions such as\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":12,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwZlAFSgBMhoAFPgLSgCsoA1KACwCUjJA7KGAGxkoADAG4gA","body":"3 * 2 * (3 - 5 + 4) + 27 / 6 * 10; "},{"body":"\n        which the interpreter would readily evaluate to be 57.  We can help\n        ourselves by writing such an expression in the form\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":13,"eval":true,"prependLength":0,"program":"chap=1&prgrm=PTAEGUEkGEAVQFLlARgHTpQKCwZlAFSgBMhoAFPgLSgCsoA1KACwCUoWTWxA7KGADYyKAAwBuIA","body":"3 * 2 * (3 - 5 + 4) \n+ \n27 / 6 * 10; "},{"body":"\n        to visually separate the major components of the expression.\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p8","child":[{"body":"\n    Even with complex expressions, the interpreter always operates in the\n    same basic cycle: It reads\n    a statement typed by the user,\n    evaluates the\n    statement,\n    and prints the result. This mode of operation is often expressed by saying\n    that the interpreter runs in a\n    ","tag":"#text"},{"tag":"EM","child":[{"body":"read-evaluate-print loop","tag":"#text"}]},{"body":".\n      \n    Observe in particular that it is not necessary to explicitly instruct the\n    interpreter to print the value of the\n    \n\tstatement.","tag":"#text"},{"tag":"FOOTNOTE_REF","id":"#footnote-link-1","body":"1","href":"/sicpjs/1.1.1#footnote-1"}]},{"tag":"DISPLAYFOOTNOTE","id":"#footnote-1","count":1,"href":"/sicpjs/1.1.1#footnote-link-1","child":[{"body":"JavaScript obeys the convention that every\n\t\n\tstatement has a value (see exercise ","tag":"#text"},{"tag":"REF","body":"4.8","href":"/sicpjs/4.1.2#ex-4.8"},{"body":").\n\tThis convention, together with the\n\treputation of JavaScript programmers as not caring about efficiency, leads\n\tus to paraphrase a quip on Lisp programmers by Alan Perlis (who was himself\n\tparaphrasing Oscar Wilde): ","tag":"#text"},{"tag":"EM","child":[{"body":"JavaScript programmers know the value of\n\teverything but the cost of nothing.","tag":"#text"}]}]}]