IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
JavaScript
Search JavaScript Group:

Advanced
-Collapse +Expand JavaScript Store

Prestwood eMagazine

November Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...JavaScript a...Beginners Co...   Print This     
  From the November 2015 Issue of Prestwood eMag
 
JavaScript Beginners Corner:
JavaScript End of Statement (; is optional)
 
Posted 16 years ago on 12/14/2008 and updated 1/19/2009
JavaScript Code Snippet:
 A flashcard from our JavaScript Flashcards Library
 A code snippet from our JavaScript Code Snippets Page

KB101690

Languages Focus: End of Statement

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

JavaScript End of Statement

In JavaScript, using a semicolon at the end of statements is optional. You might think a semicolon then is just another comment specifier but it is not. The semicolon is an optional end of statement specifier. To put two statements on a single code line, you must use a semicolon. However, the semicolon is optional, but probably confusing, when you break a single statement into multiple code lines.

Syntax Example:
document.write("Hello1");
document.write("Hello2");

//Semicolons are optional:
document.write("Hello3")
document.write("Hello4")

//This works too but only if you use a semicolon:
document.write("Hello5"); document.write("Hello6");

//Two lines also works:
document.write
("Hello7")

Multiple Statements on a Single Code Line

You can put multiple statements on a single code line so long as you end each statement with a semicolon. The last semicolon is optional.

For example:

document.write("Hello8");  document.write("Hello9"); document.write("Hello10")
document.write("Hello11");

Notice no semicolon after document.write("Hello10").

JavaScript End of Statement Recommendation

Should I use a semicolon or not?

Because the semicolon is optional, many JavaScript coders opt to not use it because it's extra typing. This is especially true for developers coming from Visual Basic, ObjectPAL, and other languages that use a return for an end of statement. Developers coming from Delphi, C++, C#, C++, and other languages that use a semicolon tend to use a semicolon consistently with JavaScript. My recomendation to you is to always use the semicolon for clarity and is required under some situations (like putting two statements on a single code line).

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

Hi Mike, my name is Amador Raga and I believe I have an account with PrestwoodBoard many years ago. Just this morning, I felt homesick on using Delphi when I got an Ad that Delphi is dead. 

Anyway, I was surprised when I saw your comment that semicolon at the end of Javascript statement is optional. It may be optional in some cases but NOT in this:

//Semicolons are optional:
document.write("Hello3")
document.write("Hello4")

---
Amador Raga
Posted 13 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P142A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101690 Counter
11953
Since 12/14/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]