Running a JavaScript Code File with 'jrunscript'

This section provides a tutorial example on how to run a JavaScript code file with 'jrunscript' in batch mode.

Probably the best way to use "jrunscript" is to run it in batch mode, where is JavaScript code file is specified at the command line. "jrunscript" will process the entire code file in one shot.

In order to test "jrunscript" in batch mode, I revised one of my existing JavaScript Web page file into a pure JavaScript code file, For_Loop_Statements.js:

// For_Loop_Statements.js
// Copyright (c) 2013 HerongYang.com. All Rights Reserved.

   var i, j, is_prime;
   for ( i=3; i<=30; i+=2 ) {
      is_prime = true;
      for ( j=2; j<=i/2; j++) {
         is_prime = i%j > 0;
         if (!is_prime) break;
      }
      if (is_prime)
//       document.write("Found a prime number: " + i + ".\n");
         print("Found a prime number: " + i + ".\n");
   }

Notice that, I have to change "document.write()" method to "print()" method, because "document" object is only available, if the script is executed in a Web browser. Here is how I ran this script file with "jrunscript":

C:\herong>\progra~1\java\jdk-10.0.1\bin\jrunscript \
   -f For_Loop_Statements.js

Found a prime number: 3.
Found a prime number: 5.
Found a prime number: 7.
Found a prime number: 11.
Found a prime number: 13.
Found a prime number: 17.
Found a prime number: 19.
Found a prime number: 23.
Found a prime number: 29.

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

'jrunscript' - JavaScript Shell Command from JDK

 Downloading and Installing JDK 10

 "jrunscript" - Scripting Shell Command and Options

 Running JavaScript Code with 'jrunscript'

 Evaluating JavaScript Code with 'jrunscript' Interactively

Running a JavaScript Code File with 'jrunscript'

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 AJAX (Asynchronous JavaScript And XML)

 References

 Full Version in PDF/EPUB