Syntax basics

Computer Science · JavaScript

Question 1 of 10

0

    How many lines does this JavaScript code print? console.log(33); // console.log(80); console.log("see // 17"); /* console.log(51); */ console.log(52); console.log(15);

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Syntax basics explained

    A JavaScript program is a list of statements that the engine runs from top to bottom. Statements usually end with semicolons, names are case-sensitive, and comments are ignored when the program runs.

    Line comments start with two slashes. Block comments are wrapped in slash-star and star-slash and can span several lines. console.log writes a value to the console, and when given several arguments it prints them on one line separated by spaces.

    // a line comment
    let a = 2; let b = 5; console.log(a + b);
    /* a block comment
       that the engine skips */
    console.log("A" + "B");
    

    Caveat: JavaScript inserts semicolons automatically at many line breaks, but relying on this causes surprises, so most style guides write them explicitly. Names must match exactly, so console.log and Console.log are different.

    Worked example

    Trace the program above.

    1. The first line is a comment and is ignored.
    2. The second line holds three statements: two declarations and a print. The print shows a + b, which is 2 + 5 = 7.
    3. The block comment is skipped.
    4. The last line prints "A" + "B", which is AB.

    Output: two lines, 7 and AB.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      How many lines does this JavaScript code print?

      console.log(37); console.log(37); console.log("see // 95"); console.log(84);

    • Medium

      Exactly one of these four lines is a SyntaxError when it is run on its own. Which one?

      function f() {} const g = () => 3; let h = { k: 1 }; int z = 3;

      • const g = () => 3;
      • let h = { k: 1 };
      • int z = 3;
      • function f() {}
    • Hard

      What does this JavaScript code log?

      function f() { return 23; } console.log(f());

      • 0
      • NaN
      • 23
      • undefined