Computer Science

Javascript Statements

JavaScript statements are individual instructions that make up a program. They can perform actions, declare variables, control flow, and more. Each statement typically ends with a semicolon, and multiple statements can be grouped together within curly braces to form a block of code.

Written by Perlego with AI-assistance

4 Key excerpts on "Javascript Statements"

  • Book cover image for: HTML, CSS & JavaScript in easy steps
    The JavaScript interpreter ignores tabs and spaces (“whitespace”) so you should use spacing to make your code more readable. For example, when adding two numbers:
    total = 100 + 200
    rather than
    total = 100+ 200
    Javascript Statements are often grouped together within { } curly brackets (“braces”) in function blocks that can be repeatedly called to execute when required. It is good practice to indent those statements by two spaces to improve readability, like this:
    {
    statement
    statement
    statement
    }
    The JavaScript keywords are described here and you will learn about operators, values, and expressions later.
    An “expression” produces a value, whereas a “statement” performs an action. Use the space bar to indent statements, as tab spacing may be treated differently when viewing the code in text editors. The rules that govern the JavaScript language is called “syntax”, and it recognizes two types of values – fixed and variable. Fixed numeric and text string values are called “literals”:
    Number literals – whole number integers, such as 100 , or floating-point numbers such as 3.142 .
    String literals – text within either double quotes, such as “JavaScript Fun” , or single quotes such as ‘JavaScript Fun’ .
    Variable values are called, quite simply, “variables” and are used to store data within a script. They can be created using the JavaScript let keyword – for example, let total creates a variable named “total”. The variable can be assigned a value to store using the JavaScript = assignment operator, like this:
    let total = 300
    Other JavaScript operators can be used to form expressions that will evaluate to a single value. Typically, an expression may be enclosed within ( ) parentheses like this expression that comprises numbers and the JavaScript +
  • Book cover image for: JavaScript All-in-One For Dummies
    • Chris Minnick(Author)
    • 2023(Publication Date)
    • For Dummies
      (Publisher)
    If you have never done any programming, you might find JavaScript to be a little strange at first, but once you know the basics, you can read and write it with ease. Listing 1-1 shows a simple JavaScript program that takes any normal name (whether it’s your name, your dog’s name, or your parakeet’s name) and turns it into the name of a new programming language by adding Script to the end of it. 30 BOOK 1 JavaScript Fundamentals LISTING 1-1: The very useful JavaScript Name Creator program let normalName = 'Chris'; let javaScriptName = normalName + 'Script'; console.log('Your JavaScript Name is ' + javaScriptName); Although this first program is simple, it demonstrates a lot of fundamental JavaScript principles. JavaScript is made of statements Just as a sentence in a human language is a fundamental building block of any piece of writing, JavaScript has a similar fundamental building block, which is called a statement. Listing 1-1 has three separate statements. Each statement ends with a semicolon, and running a statement causes JavaScript to do something. In the case of the first two statements, which begin with let, these are telling JavaScript to store a value. The third statement, which begins with console.log, is telling JavaScript to log (or write) some text to the console in your browser. Just as English is made up of parts of speech — such as nouns, verbs, and adverbs, Javascript Statements are made up of several parts, as described in this list: » Values: In Listing 1-1, 'Chris' is a value. » Expressions: Expressions are units of code that are evaluated and become values. For example, in Listing 1-1, the following is an expression: normalName + 'Script' When normalName has a value of 'Chris', this expression evaluates to 'ChrisScript'. » Operators: Operators do something with values. In Listing 1-1, the + operator (known as the concatenation operator) joins together the value of normalName and the literal value 'Script'.
  • Book cover image for: Start Programming Using HTML, CSS, and JavaScript
    With di ff erent combinations of processing and decision boxes, you can assemble any algorithm you like. Maria : We still don’t have repetitions. Professor : You can connect an arrow exiting a box to the entrance of an arbitrary box. If one of the arrows exiting a decision box is directed backwards, you get a repetition, as you will soon see. Expression Statement Professor : As already mentioned, the simplest statement in JavaScript is an expres-sion that has a side e ff ect terminated with a semicolon, which is called an expression statement. For example, if you want to increase the value of counter by one, the next statement will do it: counter++; //Increments counter We can draw a flowchart for this statement, which looks like this. counter++ Another example of an expression statement is a function call. For example: 130 Meeting 7. Controlling Program Flow document.write(counter); Compound Statement Professor : There are often situations when you need to write several statements while the rules only allow one. On such occasions you use a compound statement , which is no more than a sequence of statements joined into a single one using a pair of curly brackets. For example: { counter++; x -= counter; } Notice that a compound statement doesn’t end with a semicolon. Maria : You indented the lines of code inside the curly brackets. Is this necessary? Professor : No, it isn’t. Indentation is completely optional, but every programmer uses it to optically separate statements that are subordinate to another statement. If the hierarchy of statements is clearly visible from the code, the code is much easier to read, understand, and, most importantly, maintain. The flowchart of the above compound statement looks like this. counter++ x -= counter Notice that from the outside, a compound statement looks like a single statement with a single entrance and exit, which is indicated by a dashed rectangle.
  • Book cover image for: Web Programming
    eBook - PDF

    Web Programming

    Building Internet Applications

    • Chris Bates(Author)
    • 2014(Publication Date)
    • Wiley
      (Publisher)
    A block of code is a set of instructions that are to be executed together as a unit. This might be because they are optional and dependent upon a Boolean condition or because they are to be executed repeatedly. • Functions have parameters which are passed inside parentheses. • Variables are declared using the keyword var. • Scripts require neither a main function nor an exit condition. These are major differences between scripts and proper programs. Execution of a script starts with the first line of code and runs until there is no more code. Many people set their browsers so that JavaScript and other programming languages are barred from opening new windows. This is a perfectly sensible thing to do as it prevents Websites opening lots of popup windows and polluting the Web experience. The code you have just seen writes to an alert box which is a standard component of Web browsers rather than a new window. The code could be modified to write to a new Window as you will see in Section 8.2. If you want to see how to write into the existing document window try this alternative version of the describe() function. 1 function describe() { var major = parseInt(navigator.appVersion); var minor = parseFloat(navigator.appVersion); var agent = navigator.userAgent.toLowerCase(); 5 document.write("

    Your Browser

    "); document.write("

    " + agent + " " + major + "

    "); document.close(); } 6.3.2 JavaScript and the HTML Page Having written some JavaScript, you need to include it in an HTML page. You cannot execute these scripts from a command line as the interpreter is part of the browser. The script is included in the Web page and run by the browser, usually as soon as the page has been loaded. The browser is able to debug the script and can display errors. 6.3. JavaScript – The Basics 147 NOTE To get Mozilla to show errors, type javascript: in the location box. A console will appear which will display the errors, although they may not be stunningly useful.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.