• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » Tutorial » JavaScript statements
Next →
← Prev

JavaScript statements

By Dinesh Thakur

A JavaScript Statement is a basic programming unit, usually representing a single step in a JavaScript program. Think of a JavaScript Statement as a sentence: Just as you string sentences together to create a paragraph (or a chapter, or a book), you combine JavaScript Statements to create a JavaScript program. For example:

alert ('Hello World!'); 

This single JavaScript Statement opens an alert window with the message “Hello World!” in it. In many cases, a JavaScript Statement is a single line of code. Each JavaScript Statement ends with a semicolon—it’s like a period at the end of a sentence. The semicolon makes it clear that the step is over and that the JavaScript interpreter should move onto the next action.

The general process of writing a JavaScript program is to type a statement, enter a semicolon, press Return to create a new, blank line, type another JavaScript Statement followed by a semicolon, and so on and so on until the program is complete.

If multiple JavaScript Statements appear on the same line, though, you must use the semicolon to terminate each one:

var bValue = true; 
var sValue = "this is also true" 

However, not explicitly terminating each JavaScript statement is a bad habit to get into, and one that can result in unexpected consequences. For instance, if you use a tool to compress the whitespace in your JavaScript code, that tool can have problems with statements that are not explicitly terminated.

The use of whitespace in JavaScript has little impact on the processing of the code, though it can impact both the readability of the code and the size of the JavaScript file. For instance, the following two lines of code are interpreted in exactly the same way:

var firstName = 'Shelley'       ; 
var firstName = 'Shelley'; 

Other than to separate words within quotes or to terminate statements, extra whitespace—such as tabs, spaces, and newlines is disregarded. In the following code, the variable assignment completes successfully, even though line breaks separate the statement:

var 
firstName= 
'Shelley'; 

The line break isn’t considered a statement terminator in this instance because JavaScript assignments require a variable name on the left, an assignment operator (=), and a value on the right. The processing application continues to process the JavaScript until either the semicolon is reached or the JavaScript Statements is completed.

The following code is also successful, because when a new variable is not assigned a value immediately, it is assigned a value of “undefined.” The new variable assignment then triggers the processing application to finish processing the previous statement:

var firstName 
var lastName; 

In the following code, though, an error results because the JavaScript processing application recognizes that the var keyword begins a new JavaScript Statement, yet the previous statement is not complete:

var firstName = 
 var lastName = 'Powers'; 

Whitespace helps an application’s readability, but it also adds to the file size. Normally, this isn’t a problem as most of our JavaScript files are relatively small. However, with some larger Ajax applications and more complex JavaScript libraries, the JavaScript can actually be quite large.

The Assignment Statement

The most common JavaScript statement is the assignment statement. It’s an expression consisting of a variable on the left side, an assignment operator (=), and whatever is being assigned on the right. The expression on the right can be a literal value:

nValue = 35.00;

Or, it can be variables and literals combined with any number of operators:

nValue = nValue + 35.00;

The right side of the JavaScript statement can also be a function call:

nValue = someFunction();

You can include more than one assignment on a line, as long as you separate the statements with a semicolon:

var firstName = ‘Shelley’; var lastName = ‘Powers’;

You can also assign the same value to several variables at once:

var firstName = lastName = middleName = “”;

However, with the following, where variables are lined up with commas between them, the first variable is set and the second ends up undefined:

var nValue1,nValue2 = 3; // nValue2 is undefined

You can concatenate assignments with commas, however:

var nValue1=3, nValue2=4, nValue3=5;

For readability and to ensure that no bugs creep in, I recommend that you separate your assignments with semicolons.

You’ll also like:

  1. JavaScript If…Else Statements
  2. Conditional Statements in C++ or if Statements
  3. JavaScript Operators | Types of JavaScript Operators
  4. JavaScript Comments
  5. JavaScript Functions
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW