Since its release in December 4, 1995 [six months after Java, which was released in May 23, 1995], JavaScript has gone through many changes. JavaScript began as a client-side programming language [which runs inside a web browser via a built-in JavaScript engine] for adding interactive contents to the web pages. It became more robust with DHTML [1997] and Ajax [1999]. With Node.js [released in May 27, 2009], JavaScript can be used to program server-side and build full-stack web applications, as well as standalone utility scripts. In 2015, the ECMAScript 6 [ES6 or ES2015] introduces major update to the language, such as classes and modules, and make JavaScript a general-purpose programming language capable of complex software tasks.
Introduction
It began as a Client-side Programming Language run inside a web browser via a Built-in JavaScript EngineJavaScript is the most widely used client-side programming language that lets you supercharge your HTML with interactivity, animation and dynamic visual effect for better User Interface and User Experience [UI/UX]. It is:
- a small, lightweight, object-oriented, cross-platform, special-purpose scripting language meant to be run under a host environment [typically a web browser].
- a client-side scripting language to enrich web user-interfaces and create dynamic web pages [e.g., for...input validation, and immediate response to user's actions].
- the engine that supports AJAX [Asynchronous JavaScript and XML - that can be used to update one part of the web page asynchronously], which generate renew interest in JavaScript.
JavaScript works together with HTML/CSS. HTML provides the contents; CSS specifies the presentation; and JavaScript programs the behavior. Together, they enrich the UI/UX of the web users.
JavaScript is Now Everywhere with Node.jsJavaScript has grown beyond the client-side programming. With the introduction of Node.js in 2009 [an open-source, cross-platform JavaScript run-time environment], you can run your JavaScript standalone or inside the server [instead of a browser]. This allows you to use one single language for both the server-side and client-side programming.
History and VersionsJavaScript, originally called LiveScript, was created by Brendan Eich at Netscape in 1995. Soon after, Microsoft launched its own version of JavaScript called JScript. Subsequently, Netscape submitted it to ECMA [formerly "European Computer Manufacturers Association", now "Ecma International - European association for standardizing information and communication systems"] for standardization, together with Microsoft's JScript.
The ECMA Specification is called "ECMA-262 ECMAScript Language Specification" @ //www.ecma-international.org/publications/standards/Ecma-262.htm [also approved as "ISO/IEC 16262"]:
- ECMA-262 version 1 [June 1997]: First edition
- ECMA-262 version 2 [August 1998]
- ECMA-262 version 3 [December 1999]: Added regular expressions, try/catch, switch, do-while, etc.
- ECMA-262 version 4 - Abandon due to political differences. In 2007, the TC-39 [the committee responsible for ECMAScript] put up a draft specification for ECMAScript 4, which was massive in scope and introduced many new syntax and features. But a group of developers from Yahoo, Google and Microsoft felt that was too much and created an alternate proposal called ECMAScript 3.1. ECMAScript 4 was never finalized.
- ECMA-262 version 5 and 5.1 [June 2011]: ECMAScript 3.1 was eventually standardized as ECMAScript 5. Added "strict mode", JSON, Array iteration methods, etc.
- ECMA-262 2015: most popularly known as ECMAScript 6 or ES6.
This version added significant new syntax for writing complex application, including class declaration,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 for local declarations,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 for constant local declaration, default parameter values, iterators and for...of loops, Python-style generators, arrow function expression [if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
9], binary data, typed arrays, new collections [maps, sets and WeakMap], promises, reflection, proxies, template literals for strings, and many more. - ECMAScript 2016 [ES7]: Since ES6, ECMAScript standards are on yearly release cycles in June.
This version added exponential operator [
0], block-scoping of variables and functions,JavaScript Example: Loop
Testing Loop
1 andJavaScript Example: Loop
Testing Loop
2 keywords for asynchronous programming, etc.JavaScript Example: Loop
Testing Loop
- ECMAScript 2017 [ES8]: Added string padding, async functions which use generators and promises,
3,JavaScript Example: Loop
Testing Loop
4 for easy object manipulation, etc.JavaScript Example: Loop
Testing Loop
- ECMAScript 2018 [ES9]: Added rest parameters [
5], spread operator, asynchronous iteration, additions to regular expression.JavaScript Example: Loop
Testing Loop
- ECMAScript 2019 [ES10]: Added
6,JavaScript Example: Loop
Testing Loop
7, changesJavaScript Example: Loop
Testing Loop
8 andJavaScript Example: Loop
Testing Loop
9, catch binding becomes optional, etc.JavaScript Example: Loop
Testing Loop
- ECMAScript 2020 [ES11]: introduces a
for [ initialization ; test ; post-processing ] { body ; }
0 primitive type for arbitrary-size integers, the nullish coalescing operator [for [ initialization ; test ; post-processing ] { body ; }
1] and thefor [ initialization ; test ; post-processing ] { body ; }
2 object. - ECMAScript 2021 [ES12]: Added enhancement to strings [
for [ initialization ; test ; post-processing ] { body ; }
3], promises [for [ initialization ; test ; post-processing ] { body ; }
4], and object references. Logical assignment operators [for [ initialization ; test ; post-processing ] { body ; }
5,for [ initialization ; test ; post-processing ] { body ; }
6,for [ initialization ; test ; post-processing ] { body ; }
7,]
Java is a full-fledged general-purpose programming language. It was created by James Gosling at Sun Microsystems [now part of Oracle] and released in August 1995.
JavaScript was created by Brendan Eich at Netscape, also in 1995. Originally called LiveScript, it was a small and lightweight special-purpose language for writing client-side program running inside a browser to create active user-interface and generate dynamic web pages. It was renamed to JavaScript in an ill-fated marketing decision to try to capitalize on the popularity of Java language, when Netscape released its Navigator 2 in 1996.
Java and JavaScript are totally different languages for different programming purposes. However, in the early days, some efforts were made to adopt Java syntax and convention into JavaScript, such that JavaScript seems to be a subset of Java. In reality, they have very little in common.
What Client-side JavaScript CANNOT Do?Remember that JavaScript is a client-side program that you downloaded from a server, and run inside the browser of your [client] machine. What to stop someone from writing a JavaScript that wipes out your hard disk, or triggers a denial-of-service attack to another server? As a result, for security purpose,
- It cannot read file from the client's machine.
- It can only connect to the server that it come from. It can read file from the server that it come from. It cannot write file into the server machine.
- It cannot connect to another server.
- It cannot close a window that it does not open.
jQuery is a popular cross-browser JavaScript Library. jQuery is JavaScript [but having its own extension syntax], hence, you need to understand JavaScript. I suggest you read through the JavaScript syntax [and pay particular attention to objects], and then switch into jQuery for your production. Read "jQuery Basics".
Standalone and Server-side JavaScripts with Node.js JavaScript Engine[TODO]
JavaScript Source-Code Editors and IDEsYou need a text editor to write your JavaScript. You could use a plain-text editor such as Windows' NotePad or macOS's TextEdit [strictly NOT recommended as they can't do syntax highlighting]. To improve your productivity, a good source-code editor [which provides syntax highlighting, auto-code-complete, snippets, documentation, symbol navigation, refactoring, etc.] is essential. There are many freeware/shareware available, such as Visual Studio Code [VS Code], Sublime Text, Atom, Brackets, Komodo Edit, Emacs, Vim, BBEdit, TextMate, NotePad++ [Windows], etc. You can also use a full-scale IDE such as Eclipse, NetBeans, IntelliJ IDEA. There are also many "online" editors and compilers available [e.g., Programiz, JSFiddle, Playcode], but I think that you should install your own.
Client-Side JavaScript by Examples
I shall assume that you know HTML and CSS [read my HTML/CSS articles otherwise]. I shall also assume that you understanding some programming basics [computational thinking] such as variables, if-else and for-loop constructs.
Client-side JavaScripts run inside a browser via a built-in JavaScript engine. There are standards on JavaScript. But the Big-5 [Chrome, Firefox, IE/Edge, Safari and Opera], in particular the IE, does not adhere to all the standards strictly. Furthermore, they create their own extensions. Hence, the behavior of JavaScript could be different in different browsers. You may need to test your JavaScripts on more than one browsers.
JavaScript also run standalone [and in the server]. To run JavaScript standalone, you need to install Node.js, which is a JavaScript engine. I will present the examples in the next section.
Client-side JS EG 1: Functions for [ initialization ; test ; post-processing ] {
body ;
}
8 and for [ initialization ; test ; post-processing ] {
body ;
}
9
Let us write our first client-side JavaScript to print the message "Hello, world".
Start with a new file and enter the following codes. Do not enter the line numbers, which is used to aid in explanation. Take note that:
- JavaScript is case sensitive. A
0 is NOT aJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
1 and is NOT aJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
2.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- "Extra" white spaces [blanks, tabs and newlines] are ignored. That is, multiple white spaces is treated as a single blank character. You could use them liberally to make your program easier to read.
Save the file as "
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
3" [or any filename that you prefer, with file extension of "
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
4" or "
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
5"]. Run the script by loading the HTML file into a JavaScript-capable browser [e.g., One of the BIG FIVE - Chrome, Firefox, Internet Explorer/Edge, Safari or Opera].
JavaScript Example: Functions alert[] and document.write[]
My first JavaScript says:
How it Works?- JavaScripts are programming codes that are embedded inside an HTML document. The codes are contained between a pair of
6 tags, as follows: NOTE: In HTML4/XHTML, you need to include attributeJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
7 to theJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
6 elements. Lines 7-9 and Line 13-17 are two pieces of JavaScripts, placed in theJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
9 andJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
0 sections, respectively.JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- JavaScript statements are terminated by a semicolon
1 [like Java/C/C++/C#].JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- The
2 function [Line 8] pops out a dialog box displaying theJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
3 and a OK button. Strings are enclosed by a pair of double quotes or single quotes, e.g.,JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
4 orJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
5.JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- The current web page is represented by the so-called
6 object in the JavaScript. TheJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
7 [Line 16] property stores the last modified date of the current document. TheJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
8 function [Line 14 to 16] can be used to write the specifiedJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
3 to the current document, as part of the current HTML document.JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- The
00 operator [Line 16] can be used to concatenates two strings [like Java].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- As a result of the
for [ initialization ; test ; post-processing ] { body ; }
9, the
0 section of this document contains:JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- The
2 andJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
8 are some of the commonly-used built-in functions provided in JavaScript.JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
TRY: Print the document's title and the URL location. [Hints: use
05 andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
06 properties.]My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Don't Get Caught by the Cache - Do Control-Refresh [or Control-F5]
If you modify the codes and reload the web page, the new codes may not get executed because the browser caches the previously loaded version. You could use Control-F5 [Control-Refresh] to ask the browser to discard the cache, and fetch a new page.
Don't Use
for [ initialization ; test ; post-processing ] { body ; }8
Because nobody like it.
"The use of alerts is usually discouraged in favour of other methods that do not block users from interacting with the page - in order to create a better user experience. Nevertheless, it can be useful for debugging."
for [ initialization ; test ; post-processing ] { body ; }8 is technically a
09 object's property, and shall be referenced asMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
10. But sineceMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
09 is a global object, all its varaibles are global variables, and can be referenced without the object name.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Browsers' F12 Web Development Tools and Debugger
Before we proceed to the next example, you should try out the Web Development Tools, which is available in most of the browsers for debugging HTML/CSS/JavaScript/webapps.
- Launch your browser and push F12 to activate the Web Development Tools [I have tried on Firefox, Chrome, Edge].
- Select the "Console" pane. You can enter JavaScript statements in the console. For example, enter the following JavaScript statements one-by-one and observe the results.
alert['hello, world'] document.write['
hello world, again
'] console.log['hello world, again and again']
Notes:
12 writes the msg to the console, which is often used for debugging [likeMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
13 in Java, orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
14 in C]Where are the error messages?My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
If you make any syntax errors in the previous example, you will not see any error messages appear on the browser, but the script simply doesn't run. It is impossible to debug a program without the error messages! Thankfully, you can actually find the error messages in the console. Make some errors and refresh the browser.
Client-Side JS EG 2: Variables and Functions My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
15, My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
16
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
This script prompts the user for his/her name, confirms the name, and prints a greeting message.
There are three kinds of pop-up dialog boxes for interacting with the users:
- The
2 function puts theJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
3 on a pop-up box with a OK button. User needs to click the OK button to continue.JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- The
19 function puts up an input pop-up box with theMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
20 with an OK and Cancel buttons. It returns the input entered by the user as a string; or a special value calledMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
21 if the user hits the Cancel button. The optional parameterMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22 specifies the initial string to be shown. In this article, I shall indicate optional function parameters with a trailingMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
23 for compactness.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- The
24 function putsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
25 on a pop-up box with OK and Cancel buttons. It returnsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
26 if user hits the OK button; orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
27 otherwise.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
How it Works?- Line 8 declares a global variable called
28, via the keywordMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
29. A variable is a named storage location that holds a value. Once the variable is declared, you can assign [and re-assign] a value to that variable, via the assignment operatorMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30 [Line 9].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Line 9 invokes the
19 function to pop out a dialog box, and reads in the string entered by the user. The string read is assigned to the variableMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
28. The functionMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
15 is similar to theMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
for [ initialization ; test ; post-processing ] { body ; }
8, but it accepts a user's input. - In Line 10, the
24 function puts up the message and returns eitherMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
26 orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
27, depending on whether the user hits the OK or Cancel button.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- If the result is
26, Line 11 printsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
39. Otherwise, Line 13 printsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
40.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
TRY: Instead of printing the greeting message using
for [ initialization ; test ; post-processing ] { body ; }9, do it via an
for [ initialization ; test ; post-processing ] { body ; }8.
Client-Side JS EG 3: The My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
43 Object
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The following script creates a
43 object representing the current date-time, and prints the current time.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: The Date object
How it Works?- Line 8 declares a variable called
45. It also creates aMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
43 object [via theMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
47 operator], which contains the current date-time stamp, and assign it toMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
45.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- "
49" begins an end-of-line comment [Lines 8 and 9]. Comments are ignored by the JavaScript engine but important in explaining your codes to others [and to yourself three days later].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Line 9 declares a variable called
50. It also invokes the functionMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
51 on objectMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
45, in the form ofMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
53, to retrieve the hour part of objectMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
45, and assign it to variableMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
50. Lines 9 and 10, similarly, handle the minutes and seconds.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Line 12 to 14 use
56 to write to the current document.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
57 [write-line] writes a newline [My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
58] after the given string. Take note that browser ignores extra white space [newlines, blanks, tabs] in an HTML document; you need to write aMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
59 orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
60 tag to ask the browser to display a line break.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Lines 16-20 contains a conditional if-then-else statement. Depending on the value of
50, one of the messages will be displayed.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Modify the above script to print the current date, month, year and day of the week. [Hints: Use functions
62,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
63,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
64 andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
65 of aMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
43 object.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
62 returns 1-31.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
63 returns 0 to 11 for January to December.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
64 returns a 4-digit year.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
65 returns 0 to 6 for Sunday to Saturday].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Use a conditional statement to print the day of the week in word [i.e., 0 for Sunday, 1 for Monday and etc.]. [Hints: Use the
71 construct as follow.]My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
Client-Side JS EG 4: Loops
The following script prompts the user for a multiplier, and prints the multiples of 1 to 100 using a for-loop.
JavaScript Example: Loop
Testing Loop
How it Works?- Line 11 prompts the user for a number, and assigns it to the variable
72.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Lines 12-14 contain a for-loop. A for-loop takes the following syntax:
for [ initialization ; test ; post-processing ] { body ; }
There are four parts in a for-loop. Three of them, initialization, test and post-processing, are enclosed in brackets
73 and separated by 2 semicolons. The body contains the repetitive task to be performed. The initialization statement is first executed. The test is then evaluated. If the test returns true, the body is executed; followed by the post-processing statement. The test is evaluated again and the process repeats until the test returns false. When the test is false, the for-loop completes and program execution continues to the next statement after the for-loop. The following flow chart illustrates the for-loop process:My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
In this example, the variable
74 is initialized to 1. IfMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
74 is less than or equal to 100, the body of the loop executes, followed by the post-processing statement, which increment the value ofMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
74 by 1. The loop repeats until the value ofMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
74 is NOT less than or equal to 100 [i.e., more than 100].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Modify the above script to prompt the user for the multiplier as well as the number of multiples to be printed [in two
15 statements].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Modify the above script to print only multiples that are odd number. [Hint: The modulo operator "
79" can be used to compute the remainder, e.g.,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
80 computes the remainder ofMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
81 divides by 2, which results in either 0 or 1.]My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Client-Side JS EG 5: User-defined Functions and My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
82 Event Handler
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Besides the JavaScript built-in functions such as
for [ initialization ; test ; post-processing ] { body ; }8,
15,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
85, andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
57, you can define your own functions. A function has a name and a body consisting of a set of JavaScript statements that collectively performs a certain task. It may take zero or more argument[s] from the caller and return zero or one value back to the caller.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
How it Works?- Lines 8-10 define a function called
87, via the keywordMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
88. The function invokes the built-in functionMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
89, which opens a new browser window [or tab] and loads the page "My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
90".My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Lines 15-16 create an HTML button. Clicking the button triggers the
82 event handler, i.e.,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
87 defined earlier.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
TRY: Include another button, which opens "
93".My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Client-Side JS EG 6: More Event Handlers: My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
94, My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 and My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
96
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript can be used to handle many types of events, in response to a user's action or browser's action. For example,
94: fires after browser loaded the page.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
96: fires when the user points the mouse pointer at/away from the HTML element.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
Dissecting the Program- Line 8 defines a variable
alert['hello, world'] document.write['
00, which holds the strings to be displayed in thehello world, again
'] console.log['hello world, again and again']
94 event handlers.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- In the opening tag [Line 12], we define the
94 event handler for theMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
03 event. It invokeshello world, again
'] console.log['hello world, again and again']for [ initialization ; test ; post-processing ] { body ; }
8 with the message defined earlier. - Line 13 and 14 defines the event handlers
95 andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
96 for the HTML elementMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
. The text's color will be changed to red when the user points the mouse pointer at the element [by setting the CSS style property
alert['hello, world'] document.write['
07 to red], and revert back to its original color when the mouse pointer is moved away [by resetting the CSS style propertyhello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
07 to an empty string]. The special keywordhello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
09 refer to this object.hello world, again
'] console.log['hello world, again and again']
Client-Side JS EG 7: Separating HTML, CSS and JavaScript
The previous example works fine. You will find many such example in textbooks, especially the older textbooks. However, it has a big problem. All the HTML contents, CSS presentation styles and JavaScript programming codes are placed in a single file. For a small toy program, the problem is not serious. But when your program grows and if the HTML, CSS and JavaScript are written by different people, you will have a real challenge in maintaining the program. Remember that HTML is for the contents, CSS for presentation and JavaScript for the behavior.
Let's rewrite the example to place the HTML, CSS and JavaScript in three different files.
0My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
1My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
2How it Works?My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Placing the scripting codes inside the HTML page is not a good software engineering practice. Instead, the now-preferred approach is to place the scripts, as well as CSS styles, in external files, which can then uniformly applied to all pages in your website.
- Let's begin with the HTML file. Now, the HTML file keeps only the contents, no presentation style and nor programming logic.
The CSS style sheet is kept in an external file, referenced via the above
6 tag.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Note: In HTML4/XHTML1.0 you need to include attributealert['hello, world'] document.write['
11 in thehello world, again
'] console.log['hello world, again and again']
6 opening tag.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Also, the JavaScript programming code is kept in an external file, referenced via the above
6 is needed, although there is no content.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Note: In HTML4/XHTML1.0, you need to include attribute
7 in theJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
6JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
3How it works?My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- This HTML document contains a element with an unique
alert['hello, world'] document.write['
16, twohello world, again
'] console.log['hello world, again and again']elements, three
elements with
alert['hello, world'] document.write['
17, and threehello world, again
'] console.log['hello world, again and again']
6 with uniqueJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
alert['hello, world'] document.write['
19.hello world, again
'] console.log['hello world, again and again'] - In the user-defined function
alert['hello, world'] document.write['
20, we usehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
21 to select the element, and then modify itshello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
22 property.hello world, again
'] console.log['hello world, again and again'] - In
alert['hello, world'] document.write['
23 function, we usehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
24 to select all thehello world, again
'] console.log['hello world, again and again']elements in an array
alert['hello, world'] document.write['
25. We then use a for-loop to iterate through all the elements in the array. Thehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
26 property keeps the length of the array.hello world, again
'] console.log['hello world, again and again'] - In
alert['hello, world'] document.write['
27 function, we usehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
28 to select all thehello world, again
'] console.log['hello world, again and again']elements. We use JavaScript's
alert['hello, world'] document.write['
29 loop to iterate through all the elements. We use the safesthello world, again
'] console.log['hello world, again and again']if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8-variable, asalert['hello, world'] document.write['
29 loop create a new block-scope variable for each iteration; but the traditionalhello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
32 does not.hello world, again
'] console.log['hello world, again and again'] - The page contains three buttons to trigger the functions defined.
- in order to manipulate elements in the DOM using JavaScript, the JavaScript code must be run after the relevant element has been created in the document. This can be done by putting the JavaScript just before the ending tag.
- The script contains a function
alert['hello, world'] document.write['
33, which is assigned as thehello world, again
'] console.log['hello world, again and again']
94 handler viaMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
35. That is,hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
33 will be triggered after the page is loaded. Thehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
33 function assignshello world, again
'] console.log['hello world, again and again']
82 event handlers to the buttons, selected viaMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
39 using the unique buttons'hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
19.hello world, again
'] console.log['hello world, again and again'] - Instead of
alert['hello, world'] document.write['
22 property, older versions of JavaScript usehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
42 [which includes both texts and markups] to modify the content of the selected element. According to MDN: "hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
22 should be used instead. Because the text is not parsed as HTML, it's likely to have better performance. Moreover, this avoids an XSS [Cross-Site Scripting] attack vector."hello world, again
'] console.log['hello world, again and again']
Client-Side JS EG 9: Intercepting a Hyperlink
This example uses a script to intercept a hyperlink to put up a warning message, then proceed to the link.
4My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
5How it works?My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- The HTML page has an hyperlink
6with an uniqueJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
alert['hello, world'] document.write['
19.hello world, again
'] console.log['hello world, again and again'] - The
alert['hello, world'] document.write['
33 function assigns anhello world, again
'] console.log['hello world, again and again']
82 handler to the hyperlink, selected viaMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
39.hello world, again
'] console.log['hello world, again and again'] - When the hyperlink is clicked, the
82 handlerMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
50 triggered. Ifhello world, again
'] console.log['hello world, again and again']
16 returns true, the new page will be loaded; otherwise, the current page remains.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
More Advanced JavaScript Examples
More "Advanced JavaScript Examples".
Debugging JavaScripts
A graphic debugger is a MUST in programming. Programming in JavaScript, in particular, requires a good debugger. It is because JavaScripts are interpreted and does not need to be compiled. Hence, there is no compiler to show you the syntax errors. A simple and trivial syntax error [e.g., missing bracket, mis-spelling] will render the entire script not workable. Worst still, you would not see any error message when the script did not work. Can you debug without any error message or clue?
Without a graphic debugging, the only mean to debug JavaScript is to insert
for [ initialization ; test ; post-processing ] { body ; }
8 at strategic locations to print out selected data.After modifying a JavaScript, I recommend that you use Ctrl-F5 to refresh the page, which shall load a fresh copy instead of loading from the cache. You might need to clear the browser's cache or re-start the browser, if you modification does not take effect.
F12 Web Developer ToolsIn Chrome and Firefox, you can press F12 to activate the developer Tools [hence called F12 Developer Tools].
It provides these features:
- Console: View the JavaScript error messages. Start the firebug and switch to the "Console" panel.
- Script: View and debug JavaScript. Start the firebug. Switch to the "Script" panel. "Enable" or "Reload" if necessary. You can set a breakpoint by clicking on the statement number, single step through the JavaScript statements, watch the expression, and etc.
- DOM: View the HTML DOM of the current document.
- HTML and CSS.
To debug JavaScript:
- Launch Firebug ⇒ Choose the "Script" panel to view your JavaScript, shown with green-bold line numbers.
- Refresh [Ctrl-F5] the page, and check the error console [Under "Console" ⇒ "Errors"] for syntax errors! Correct all the syntax errors.
- To trace the program, set breakpoints at selected JavaScript statements, by clicking on the right margin [to the left of line number]. A red circle shows up denoting a breakpoint. Take note that you can only set breakpoint on statements with a green-bold line number. [If your JavaScript statements do not have a green-bold line number, there are syntax errors on these statements. You need to correct the syntax errors and reload the page.]
- Trigger the script [via clicking button/link, or reloading the page]. The execution stops at the first breakpoint. You can then step-over the statement [or step-into function], and inspect the variables by positioning the mouse pointer on the variable; or add the variable to "watch".
- You can resume the execution [via the continue button].
alert['hello, world'] document.write['
53hello world, again
'] console.log['hello world, again and again']Instead of using
for [ initialization ; test ; post-processing ] { body ; }
8 andfor [ initialization ; test ; post-processing ] { body ; }
9 to show the intermediate results, you can also usealert['hello, world'] document.write['
56, which writes thehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
57 and the value of thehello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
58 to the error console. It does not interfere the appearance you web page nor your normal operations.hello world, again
'] console.log['hello world, again and again']For example, modify the JavaScript in Example 8 to print the values of innerHTML before modification. You need to turn on the console [press F12] to see the output.
6My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript and HTML
As seen from the previous examples, JavaScripts are embedded inside an HTML document, and executed by the browser. There are two occasions on which browser executes JavaScript instructions:
- JavaScripts enclosed in
6 runs during loading of the page.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Some JavaScripts, called event handlers, run as a result of the user's or browser's action [or event]. For example, clicking a button [
82] or loaded a page [My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
94].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
There are, therefore, two places to put your JavaScript:
- between
6 container tags; andJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- inside the HTML tags as the event handlers [such as
82,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
94], e.g.,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
66.hello world, again
'] console.log['hello world, again and again']
6 element contains the JavaScript programming statements. For example,JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
In HTML4/XHTML1.0, you need to include the attribute
alert['hello, world'] document.write['
68 in thehello world, again
'] console.log['hello world, again and again']
6 is needed although there is no content!?JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
In HTML4/XHTML1.0, you need to include
alert['hello, world'] document.write['
68 in thehello world, again
'] console.log['hello world, again and again']
6 tags, and run it under a web browser, you will need to activate the debugger [Web Developer Tools] to see the outputs. See the above section on client-side examples.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Standalone JS Eg 2: [TODO]- This HTML document contains a element with an unique
- HTML comments are enclosed inside .
- CSS supports multi-line comment
alert['hello, world'] document.write['
75, but NOT end-of-line commenthello world, again
'] console.log['hello world, again and again']
49.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- JavaScript supports both
alert['hello, world'] document.write['
75 andhello world, again
'] console.log['hello world, again and again']
49, like Java/C/C++/C#.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- An identifier can contain letters [
alert['hello, world'] document.write['
92,hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
93], digits [hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
94], underscore [hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
95] and dollar sign [hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
96]. But it cannot begin with a digit [hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
94].hello world, again
'] console.log['hello world, again and again'] - Identifiers are case-sensitive. A
0 is NOT aJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
2, and is NOT aJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
1.JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Identifiers cannot be keywords.
- Take note that hyphen [
01] and space are NOT allowed. Hence,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
02,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
03, are NOT valid identifiers. You should use underscore [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
alert['hello, world'] document.write['
95] instead of hyphen [hello world, again
'] console.log['hello world, again and again']
01].JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- string: a sequence of characters. Strings literals are enclosed in a pair of single quotes or double quotes [e.g.,
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13].JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- number: takes both integer [e.g.,
06] or floating-point [e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
15].JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- boolean: takes boolean literal of either
26 orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
27 [in lowercase].My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- undefined: takes a special literal value called
18. Take note thatJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 is both a type and a literal value.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- symbol [ES6]: A data type whose instances are unique and immutable.
- bigint [ES2020/ES11]: integers with arbitrary precision.
- object: for general objects.
- function: for function objects. Unlike Java, function is a first class object in JavaScript, e.g., you can assign a function to a variable.
- null: A special literal value for unallocated [unconstructed]
20. Take note thatJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
21 is NOTMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22. TheJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
23 isJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
20.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
21 is meant to represent an absence of a constructed object.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
21 is actually considered to be a primitive as it has no properties and methods.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Decimal: begins with a digit
47 toJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
48 [notJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
49], e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
50 orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
51,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Octal: begins with a digit
49, e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
53 orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
54,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Hexadecimal: begins with
55 [orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
56], e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
57 orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
58.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Binary: begins with
59 [orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
60], e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
61 orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
62. [Binary may not be supported in some browsers.]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- infinity: e.g.,
67JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- -infinity: e.g.,
68JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- NaN [Not-a-Number]: e.g.,
69, or converting the stringJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
70 to a number.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- parseInt[str], parseFloat[str]: Parse the
3 until the first non-digit, and return the number; orJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
91.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Math.round[num], Math.floor[num], Math.ceil[num]:
- Math.random[]: Generate a random number between 0 [inclusive] and 1 [exclusive].
- isNaN[str]: return true if the
3 is not a number. For example, It is interesting to note that JavaScript does not have counterpart functions likeJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
93,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
94.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Number[str]: Return the number represented by
25, orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91. Take that that this function name begins with uppercase, because this is a type casting operation.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- .toFixed[decimalPlaces]: Return this number/string to the given number of decimal places. For example,
- .length: E.g.,
48 returns the length of the string.JavaScript Example: The Date object
- .toUpperCase[]: returns the uppercase string.
- .toLowerCase[]: returns the lowercase string.
- .charAt[idx]: returns the character at the
49 position. Index begins from 0. Negative index can be used, which counts from the end of the string.JavaScript Example: The Date object
- .substring[beginIdx, endIdx]: returns the substring from
50 [inclusive] toJavaScript Example: The Date object
51 [exclusive].JavaScript Example: The Date object
- .substr[beginIdx, length]: returns the substring from
50 ofJavaScript Example: The Date object
53.JavaScript Example: The Date object
- .indexOf[searchStr, fromIdx?]: Return the beginning index of the first occurrence of
54, starting from an optionalJavaScript Example: The Date object
55 [default of 0]; orJavaScript Example: The Date object
56 if not found.JavaScript Example: The Date object
- .lastIndexOf[searchStr, fromIdx?]: Return the beginning index of the last occurrence of
54JavaScript Example: The Date object
6, starting from an optionalJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
55 [default ofJavaScript Example: The Date object
60]; orJavaScript Example: The Date object
56 if not found.JavaScript Example: The Date object
- .slice[beginIdx, endIdx]: Return the substring from
62 [inclusive] toJavaScript Example: The Date object
63 [exclusive].JavaScript Example: The Date object
- .repeat[count] [ES6]: repeat the string.
- .split[delimiter], array.join[delimiter]: returns an array by splitting the string using
64, e.g.,JavaScript Example: The Date object
- Searching/Modifying Strings using Regular Expression: See "".
- .charAt[idx]: For example,
- Array Bracket Operator []: You can treat a string as a character array, and access individual character via array bracket operator
65, e.g.,.JavaScript Example: The Date object
- number
49JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- number
91 [Not-a-Number],JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- empty string [
13,JavaScript Example: The Date object
88],JavaScript Example: The Date object
18 value,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- boolean
27,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
21 [unallocated] object.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
[TODO] Check Limitations such as File IO, Networking etc. Compare with Shell Script and Python script for automation.
Server-Side JavaScript by Examples
You can also use JavaScript to program a server-side webapp, just like Java Servlet/JSP, Microsoft ASP, PHP, Python, etc. The appeal is that you can use one single language to program both the client-side and the server-side in a webapp.
See Node.js article.
JavaScript Basic Syntax
Comments
Comments are ignored by the JavaScript runtime but greatly useful in explaining your codes to others [and also to yourself three days later]. You should use comments liberally to explain or document your codes.
An end-of-line comment begins with
49 and lasts till the end of the current line. A multi-line comment begins withMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
73 and lasts tillhello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
74.hello world, again
'] console.log['hello world, again and again']Take note that:
Whitespaces [blank, tab, newline]
Like C/C++/Java, JavaScript ignores additional whitespaces [blanks, tabs, and newlines]. I strongly recommend that you use additional whitespaces to format your program to make your code easier to read and understand.
Expressions
An expression is a combination of variables, literals, operators, and sub-expressions that can be evaluated to produce a single value.
Statements, Semicolon and Blocks
A statement is a single programming instruction. Unlike C/C++/Java, where you need to end a statement with a semicolon [
alert['hello, world'] document.write['
79], in JavaScript the semicolon is optional. However, if semicolon is missing, you need to end the statement with a newline [and JavaScript engine will insert a semicolon for you].hello world, again
'] console.log['hello world, again and again']Semicolons in JavaScript divide the community. Some prefer to use them always, no matter what. Others like to avoid them for brevity. I shall leave it to you.
A block consists of zero or more statements enclosed in a pair of curly braces
alert['hello, world'] document.write['
80. No semicolon is needed after the closing brace.hello world, again
'] console.log['hello world, again and again']Variables, Literals & Types
Variable declarations [
29,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8] and Assignment Operator [alert['hello, world'] document.write['
84]hello world, again
'] console.log['hello world, again and again']A variable is a named storage location that holds a value. Prior to ES6, you can only use
29 to declare global variables. ES6 introduces two new keywords:My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 to declare a local block-scope variable, andif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 to declare a local block-scope constant.You should try
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 [safest], followed byif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7, and avoid
29.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
You can assign [and re-assign] a value to a variable using the assignment [
alert['hello, world'] document.write['
84] operator. For example,Identifiershello world, again
'] console.log['hello world, again and again']Identifiers are names given to identify entities [such as variables and functions]. The rules for valid identifiers are:
A literal is a fixed value, e.g.,
06,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
07,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
09,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
26,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
21, that can be assigned to a variable, or form part of an expression.TypesMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript is object-oriented. But, It supports both primitive types and objects.
Primitives are not objects and do not possess properties and methods. JavaScript supports these primitive types:
JavaScript also supports these object types and value [we shall discuss object later]:
Unlike most of the general programming languages [such as Java/C/C++/C#] which are strongly type, JavaScript is loosely type [similar to most of the scripting languages such as UNIX Shell Script, Perl, Python]. You do not have to explicitly declare the type of a variable [such as
27 andJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
28] during declaration. The type is decided when a value is assigned to that variable. If a number is assigned, the variable takes on theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
74 type and can perform numeric operations such as addition and subtraction. If a string is assigned, the variable takes on theMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30 type and can perform string operations such as string concatenation. In other words, the type is associated with the value, instead of the variable.OperatorJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
31JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
You can use the operator
31 to check the type of the current value assigned to a variable.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
31 returns a string.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
For example,
The
18 Type andJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 Literal ValueJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
An undeclared variable [via
29 keyword] takes on a special type calledMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
18. You cannot refer to its value.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
When a variable is declared [via
29 keyword] without assigning an initial value, it takes on the typeMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
18 and holds a special value calledJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
41 is probably more precise?!], As soon as a value is assigned, the variable takes on the type of that value. The act of putting a value into a variable sets its type. You can change the type of a variable by re-assigning a value of another type. In other words, type is associated with the value, not the variable. Types are converted automatically as needed during execution [known as dynamically-typed].JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
For examples,
[ES6] Constantsif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8-declarationYou can create a read-only, named constant with the keyword
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 [in place of
29]. For example,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
7My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The
74 Type, Literals & OperationsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
A variable of type
74 holds a number, either an integer or a floating-point number.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Integer literals can be expressed in:
Floating-point literals can be expressed in the usual form [e.g.,
63] or scientific notation, e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
64,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
65.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
JavaScript also provides some special
74 literals:My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Arithmetic operations, as tabulated below, can be applied to numbers. The following results are obtained assuming that
71,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
72 before the operation.OperatorDescriptionExampleResult [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
71,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
72]+Additionz = x + y;z is 7-Subtraction [or Unary Negation]z = x - y;z is 3*Multiplicationz = x * y;z is 10/Divisionz = x / y;z is 2.5%Modulus [Division Remainder]z = x % y;z is 1++Unary Pre- or Post-Incrementy = x++; z = ++x;JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Same as: y = x; x = x+1; x = x+1; z = x;y is 5; z is 7; x is 7--Unary Pre- or Post-Decrementy = --x; z = x--;
Same as: x = x-1; y = x; z = x; x = x-1;y is 4; z is 4; x is 3**Exponent
[ES7]y ** xIn JavaScript, arithmetic operations are always performed in double-precision floating-points [NOT integers]. That is,
75 givesJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
76 [instead of 0 in Java/C/C++]. You may use the built-in functionJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
77 to truncate a floating-point value to an integer, e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
78 andJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
79 givesJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
80. You may also use the built-in mathematical functions such asJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
81,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
82,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
83 for converting a floating-point number to an integer.Exponent Operator [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
0]JavaScript Example: Loop
Testing Loop
ECMAScript 2016 [ES7] introduces exponent operator [
0], you can writeJavaScript Example: Loop
Testing Loop
86, e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
87.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Before ES7, you have to use built-in function
88.Arithmetic cum Assignment OperatorsJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
These are short-hand operators to combine two operations.
OperatorDescriptionExampleResult+=Addition cum Assignmentx += y;Same as: x = x + y;-=Subtraction cum Assignmentx -= y;Same as: x = x - y;*=Multiplication cum Assignmentx *= y;Same as: x = x * y;/=Division cum Assignmentx /= y;Same as: x = x / y;%=Modulus cum Assignmentx %= y;Same as: x = x % y;Some frequently-used
89 built-in FunctionsJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
The
30 Type, Literals & OperationsJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
A
30 is a sequence of characters enclosed within a pair of single quotes or double quotes [e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
01,JavaScript Example: The Date object
02]. ES6 introduces multi-line template strings delimited by back quotes [in the next section]. You can use an escape sequence to represent special non-printable characters [such asJavaScript Example: The Date object
58 for new-line,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
04 for tab, andJavaScript Example: The Date object
05 for Unicode character]; and to resolve conflict [e.g.,JavaScript Example: The Date object
06,JavaScript Example: The Date object
07,JavaScript Example: The Date object
08].JavaScript Example: The Date object
Unlike Java/C/C++, but like HTML/CSS's attributes, you can use either single quotes or double quotes for
30. This is handy as you can use single quotes if theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 contains double quotes [e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
11JavaScript Example: The Date object
11], without using the clumsy escape sequences [e.g.,JavaScript Example: The Date object
13].JavaScript Example: The Date object
JavaScript is dynamically-type, and performs type conversion automatically. When a
30 value is used in arithmetic operations [such as subtraction or multiplication], JavaScript runtime automatically converts theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 to aJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
74 if it represents a validMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
74; or a specialMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
74 calledMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91 [not-a-number] otherwise. For example,TheJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
00 Operator: Addition or Concatenation?My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
If both the operands to a
00 operator areMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22, it performs the usual numeric addition. However, if one [or both] of the operand is aJavaScript Example: The Date object
30, theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
00 operator is overloaded to perform string concatenation. The other operand will be converted to aMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30, if necessary. For example,Converting a numericJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 toJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
74:My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
77,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
29 andJavaScript Example: The Date object
30JavaScript Example: The Date object
To convert a numeric
30 to aJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
74, you could use the built-in functionsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
77 orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
29, which returns aJavaScript Example: The Date object
74 if conversion is successful; orMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91 otherwise. For example,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Take note that
77 works as long as theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 begins with digits. It will parse up to the first non-digit. For example,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
You can also use the function
30, which converts the object argument to a number that represents the object's value; orJavaScript Example: The Date object
91 if conversion fails. For example,Constructing/Converting toJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30:JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
42 andJavaScript Example: The Date object
43JavaScript Example: The Date object
You can construct a
30 from another type viaJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
42 function, e.g.,JavaScript Example: The Date object
You can use
43 to obtain aJavaScript Example: The Date object
30 representation of an object, e.g.,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
You can also convert a number to string by concatenating with an empty string, e.g.,
String's PropertiesJavaScript does not have a dedicated character type, but treats it as a one-character single. To get an individual character from a string, you can use:
[ES6] Multi-line Back-Quoted Template String and Substitution
Prior to ES6, You can enclosed a string literal using either single or double quotes [e.g.,
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
01,JavaScript Example: The Date object
02]. Escape sequences are to be used for special characters [e.g.,JavaScript Example: The Date object
58 for new-line,My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
04 for tab] and resolving conflict [e.g.,JavaScript Example: The Date object
06,JavaScript Example: The Date object
07,JavaScript Example: The Date object
08]. Multi-line strings are not supported.JavaScript Example: The Date object
ES6 introduces template string, delimited by back-quotes, for example,
There is no need to escape single or double quote inside a template string [because there is no conflicts]. But you need to use escape sequence for back-quote, i.e.,
75.JavaScript Example: The Date object
Template strings support multi-line strings. All whitespaces [blank, tab, newline] within the back-quotes are part of the multi-line string. For example,
Substitution
76JavaScript Example: The Date object
Template strings support substitution, which allow you to embed any valid JavaScript expression as part of the string in the form of
76, for example,JavaScript Example: The Date object
Multi-line template string with substitution provides a more convenient way to write out formatted HTML fragment. For example,
8My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The
78 Type, Literals & OperationsJavaScript Example: The Date object
A variable of the type
78 holds a value of eitherJavaScript Example: The Date object
26 ofMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
27.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
26 andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
27 are keywords in JavaScript.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
As mentioned, JavaScript performs automatic type conversion if necessary. During the type conversion, the following 6 values are converted to
27:My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
All the other values are converted to
26.My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
You need to memorize this:
93 returnJavaScript Example: The Date object
26 ifMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 is one of these 6 values:JavaScript Example: The Date object
49,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
91,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: The Date object
18,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
27, andMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
21.Comparison [Relational] OperatorsMy First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The following relational operators, which produce a
78 result, are defined in JavaScript. The results are obtained assumingJavaScript Example: The Date object
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
03,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
04.OperatorDescriptionExampleResult
[if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
03,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
04]==Abstract Equal To
[in Value]
9My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['
0!=Abstract Not Equal To===Strictly Equal Tohello world, again
'] console.log['hello world, again and again']
[in Type and Value]alert['hello, world'] document.write['
1hello world, again
'] console.log['hello world, again and again']alert['hello, world'] document.write['
2!==Strictly Not Equal To>Greater Than>=Greater Than or Equal Tohello world, again
'] console.log['hello world, again and again']