There are several ways to run JavaScript from the command line:
Node.js: Node.js is a JavaScript runtime that allows you to run JavaScript code on the server-side. You can run JavaScript code using Node.js by creating a JavaScript file and running it with the ‘node’ command. For example, if you have a file named ‘script.js’, you can run it with the following command:
node script.js
npx: npx is a package runner tool that comes with Node.js. It allows you to run JavaScript scripts that are installed as npm packages without the need to install them globally. For example, you can run the ‘create-react-app’ package with the following command:
npx create-react-app my-app
JavaScript shell: Some JavaScript shells like ‘js’ from the Mozilla’s SpiderMonkey engine, ‘jjs’ from Oracle’s Nashorn engine, ‘osascript’ for MacOS allow you to run JavaScript code directly in the shell.
For example, on MacOS you can run the following command:
osascript -e 'console.log("Hello World!")'
Note that the above command will only work on MacOS and you may need to install the JavaScript engine on other operating systems.
It’s also worth noting that you can use JavaScript in some task runners like Grunt, gulp, and webpack to automate repetitive tasks in your development workflow.
Also Read:
- What is JavaScript?
- What Is AJAX?
- List of JavaScript Functions
- How To Copy An Array In JavaScript
- How To Get All Unique Values In A JavaScript Array
- Convert int to string in JavaScript
- How To Check If An Object Is An Array In JavaScript
- How To Randomize An Array In JavaScript
- How To Replace All Occurrences Of A Character In A String?
- How To Split A String In JavaScript?
- How To Replace Multiple Spaces With Single Space In JavaScript
- How To Replace All Character In A String In JavaScript
- How To Check If A String Contains A Substring In JavaScript
- How To Loop Through An Array In JavaScript
- How To Check If A Value Exists In An Array In JavaScript
- How To Remove Duplicates From An Array Using JavaScript
- How To Create Multiline Strings In JavaScript
- How To Remove A Specific Element From An Array In JavaScript
- How To Define A Function In JavaScript?
- How To Concatenate Two String Arrays In JavaScript
- How To Get Image Size (Height & Width) Using JavaScript
- How To Change Image Size In JavaScript
- How To Increase and Decrease Image Size Using JavaScript
- How To Trigger Or Pause A CSS Animation In JavaScript
- How To Check If A Variable Is Undefined Or Null In JavaScript
- How To Check If A Variable Exists Or Is Defined In JavaScript
- How To Change The Background Color With JavaScript
- How To Encode A URL With JavaScript
- How To Decode A URL In JavaScript
- Difference Between encodeURIComponent() And encodeURI()
- Convert Comma Separated String Into An Array In JavaScript
- How To Check If A String Is Empty In JavaScript
- How To Sort An Array Of Numbers In JavaScript
- How To Return Multiple Values From A Function In JavaScript
- How To Get The Current URL With JavaScript
- How To Include A JavaScript File In Another JavaScript File
- How To Detect Screen Resolution With JavaScript
- How To parse JSON Into JavaScript
- How To Add Elements To An Array In JavaScript
- How To Generate A Timestamp In JavaScript
- How To Convert A JavaScript Object To JSON String
- How To Add An Element To The Beginning Of An Array
- How To Get The Value From The Input Field In JavaScript
- Adjust The iFrame Height To Fit With Content In JavaScript
- Call Two Functions From The Same onClick Event In JavaScript
- How To Detect When A Window Is Resized Using JavaScript
- How To Reset A Form With JavaScript
- How To Pass JavaScript Variables To PHP
- Difference Between PHP And JavaScript
- Armstrong Number In JavaScript
- How To Determine If A Number Is Odd Or Even In JavaScript
- How To Check If A Number Is A Palindrome In JavaScript
- How To Convert Strings To Uppercase In JavaScript
- How To Convert A String To Lowercase In JavaScript
- Code To Check If Age Is Not Less Than 18 Years In JavaScript
- How To Reverse A Number In JavaScript
- How To Check If A Number Is Prime Using JavaScript
- How To Find Factorial Of A Number In JavaScript
- Sum Of Two Numbers In JavaScript
- How To Display The Current Date And Time In JavaScript
- Digital Clock In JavaScript
- How To Redirect To Another Page After 5 Seconds In JavaScript
- How To Get The Last Character Of A String In JavaScript
- Remove The Last Character Of A String In JavaScript
- How To Validate An Email Address In JavaScript
- How To Check If An Input Field Is Empty In JavaScript
- Check If An Input Field Is A Number In JavaScript
- Confirm Password Validation In JavaScript
- How To Print A PDF File Using JavaScript
- Calculate The Number Of Days Between Two Dates In JavaScript
- How To Compare Two Dates In JavaScript
- Calculate Age With Birth Date YYYYMMDD In JavaScript
- How To Append or Add Text To A DIV Using JavaScript
- How To Get The Text Of HTML Element In JavaScript
- How To Change The Text Inside A DIV Element In JavaScript
- Show/Hide Multiple DIVs In JavaScript
- Show A DIV After X Seconds In JavaScript
- Display A JavaScript Variable In An HTML Page
- How To Generate A Random Number In JavaScript
- Bubble Sort In JavaScript
- Insertion Sort In JavaScript
- Selection Sort In JavaScript
- How To Remove A Specific Item From An Array In JavaScript
- Merge Sort In JavaScript
- Round To 2 Decimal Places In JavaScript
- SetInterval() and setTimeout() Methods In JavaScript
- Generate A Unique ID In JavaScript
- Caesar Cipher In JavaScript
- How To Reverse A String In JavaScript
- How To Loop Through A Plain JavaScript Object
- How To Open A URL In A New Tab Using JavaScript?
- How To Disable Right-click In JavaScript
- How To Format Number As Currency String In JavaScript
- Create An Associative Array In JavaScript
- How To Refresh A Page In JavaScript?
- Fibonacci Series Program In JavaScript
- How To Merge Objects in JavaScript?
- Anagram In JavaScript
- Convert Decimal To Binary, Octal, or Hexadecimal In JavaScript
- How To Calculate The Square Root Of A Number In JavaScript?
- Write A JavaScript Program to Compute GCD of Two Positive Integers?
- How To Get URL Parameters In JavaScript?
- How To Add JavaScript To A Webpage?
- What Is a Web Worker In JavaScript?
- How To Sort an Array of Strings In JavaScript?
- ECMAScript vs JavaScript
- What Is Strict Mode In JavaScript?
- Arrow Functions In JavaScript
- Difference Between Node.js and AngularJS With Example
- Difference Between == And === In JavaScript
- What Is JSON In JavaScript
- How To Go Back To Previous Page In JavaScript?
- How To Detect A Mobile Device With JavaScript?
- How To Close The Current Tab In A Browser Window Using JavaScript?
- How To Convert Input Text To Uppercase While Typing Using JavaScript?
- How To Show A Confirmation Message Before Delete In JavaScript?
- How To Detect Browser or Tab Closing In JavaScript?
- How To Get Hash Value From URL Using JavaScript?
- How To Get The Name, Size, And Type Of A File In JavaScript?