JavaScript Statements

In this chapter, we will learn about JavaScript statements. A statement in JavaScript is an instruction that the browser executes. Understanding JavaScript statements is fundamental to writing and reading JavaScript code. We will cover:

  • JavaScript Programs
  • JavaScript Statements
  • Semicolons
  • JavaScript White Space
  • JavaScript Line Length and Line Breaks
  • JavaScript Code Blocks
  • JavaScript Keywords

JavaScript Programs

A computer program is a list of "instructions" to be "executed" by a computer. In a programming language, these programming instructions are called statements. A JavaScript program is a list of programming statements.

JavaScript Statements

JavaScript statements are composed of:

  • Values
  • Operators
  • Expressions
  • Keywords
  • Comments

Each statement in JavaScript tells the browser to perform a specific task, such as declaring a variable, performing an arithmetic operation, or displaying a message.

Example

let name = "Arjun"; // Variable declaration let age = 25; // Variable declaration console.log(name); // Displaying a message console.log(age); // Displaying a message 

Semicolons

In JavaScript, semicolons (;) are used to separate statements. Although semicolons are optional, it is a good practice to use them to avoid potential errors and make the code more readable.

Example

let name = "Arjun"; let age = 25; console.log(name); console.log(age); 

JavaScript White Space

JavaScript ignores extra spaces. You can add white space to your script to make it more readable. Spaces, tabs, and newlines are considered white space.

Example

let name = "Arjun"; let age = 25; console.log(name); console.log(age); 

JavaScript Line Length and Line Breaks

For best readability, programmers often keep lines of code less than 80 characters long. If a JavaScript statement is too long, you can break it into multiple lines.

Example

let longString = "This is a very long string that " + "we are breaking into multiple lines " + "to make it more readable."; console.log(longString); 

JavaScript Code Blocks

JavaScript code blocks are used to group statements. Blocks are defined by curly braces {}. Code blocks are often used in functions, loops, and conditionals.

Example

if (age > 18) { console.log("Adult"); } else { console.log("Not an adult"); } for (let i = 0; i < 5; i++) { console.log("Iteration:", i); } 

JavaScript Keywords

JavaScript keywords are reserved words that have special meanings. Keywords are used to perform specific operations in the language and cannot be used as identifiers.

Common Keywords

  • let: Declares a block-scoped variable.
  • const: Declares a block-scoped, read-only named constant.
  • if: Marks a block of statements to be executed if a condition is true.
  • else: Marks a block of statements to be executed if the same condition is false.
  • for: Marks a block of statements to be executed in a loop.
  • function: Declares a function.
  • return: Exits a function and returns a value.
  • var: Declares a variable (older syntax).

Example

let name = "Arjun"; // let is a keyword const age = 25; // const is a keyword if (age > 18) { // if is a keyword console.log("Adult"); } else { // else is a keyword console.log("Not an adult"); } 

Conclusion

In this chapter, you learned about JavaScript statements, including how to use semicolons, white space, line length, line breaks, code blocks, and keywords. Understanding these basic elements of JavaScript syntax is essential for writing and understanding JavaScript code. In the next chapter, we will explore more advanced topics in JavaScript programming.

Leave a Comment

Scroll to Top