JavaScript Installation
JavaScript is a widely used programming language, mainly for web development. Unlike other programming languages, JavaScript usually doesn't need to be installed separately because it's already built into all modern web browsers. In this chapter, we'll introduce how to set up a JavaScript development environment.
Built-in JavaScript in Browsers
All modern web browsers (such as Chrome, Firefox, Safari, Edge, etc.) have built-in JavaScript engines, which means you can run JavaScript code without installing any additional software.
Running JavaScript in the Browser
Open the browser's developer tools:
- Chrome/Edge: Press F12 or Ctrl+Shift+I (Windows/Linux), Cmd+Option+I (Mac)
- Firefox: Press F12 or Ctrl+Shift+I (Windows/Linux), Cmd+Option+I (Mac)
- Safari: Press Cmd+Option+I (you need to enable the Develop menu in Safari preferences first)
Open the Console: Find the "Console" tab in the developer tools
Run code: Type JavaScript code in the console and press Enter to execute
console.log("Hello, World!");Text Editors
While browsers can run JavaScript, you'll need a text editor to write more complex programs. Here are some recommended editors:
Free Editors
Visual Studio Code (Recommended):
- Free code editor developed by Microsoft
- Supports multiple programming languages
- Has a rich plugin ecosystem
- Download: https://code.visualstudio.com/
Atom:
- Free open-source editor developed by GitHub
- Highly customizable
- Download: https://atom.io/
Sublime Text:
- Lightweight but powerful editor
- Fast startup
- Download: https://www.sublimetext.com/
Professional Editors
- WebStorm:
- Professional JavaScript IDE developed by JetBrains
- Full-featured, but requires payment
- Download: https://www.jetbrains.com/webstorm/
Node.js Environment
While browsers can run JavaScript, in some cases, you may need to run JavaScript code on the server side or locally. This is when you need to install Node.js.
Installing Node.js
Visit the official website: Go to https://nodejs.org/
Download the installer:
- Choose "LTS" (Long Term Support) recommended for most users
- Or choose "Current" (latest version) if you need the newest features
Run the installer:
- Windows: Download the .msi file and double-click to run
- macOS: Download the .pkg file and double-click to run
- Linux: Choose the appropriate installation method for your distribution
Verify installation: Open a command line tool (Windows: Command Prompt or PowerShell, macOS/Linux: Terminal) and enter:
bashnode -v npm -vIf version numbers are displayed, the installation was successful.
Running JavaScript with Node.js
Create a JavaScript file: Use a text editor to create a .js file, e.g., hello.js
Write code:
javascriptconsole.log("Hello, World from Node.js!");Run the file: Navigate to the file directory in the command line, then run:
bashnode hello.js
Development Environment Configuration
Visual Studio Code Configuration
If you chose Visual Studio Code as your editor, you can install the following plugins to enhance your JavaScript development experience:
- ESLint: Code checking tool that helps you follow best practices
- Prettier: Code formatting tool that keeps code style consistent
- Auto Rename Tag: Automatically rename HTML tags
- Bracket Pair Colorizer: Colorful bracket matching for easier code reading
- JavaScript (ES6) code snippets: ES6 code snippets to improve coding efficiency
Browser Developer Tools
Modern browser developer tools provide powerful debugging features:
- Elements: View and edit HTML and CSS of web pages
- Console: Run JavaScript code and view logs
- Sources: Debug JavaScript code, set breakpoints
- Network: Monitor network requests
- Performance: Analyze web page performance
- Application: View local storage, cache, etc.
Your First JavaScript Program
Let's create a simple HTML file to run JavaScript:
Create an HTML file: Create a new file named index.html
Add the following code:
html<!DOCTYPE html> <html> <head> <title>My First JavaScript Program</title> </head> <body> <h1>Welcome to JavaScript</h1> <button onclick="showMessage()">Click Me</button> <script> function showMessage() { alert("Congratulations! You have successfully run your first JavaScript program!"); } </script> </body> </html>Save and open the file: Open this HTML file in a browser, click the button to see the result
Summary
JavaScript installation and setup is relatively simple:
- Browsers have built-in JavaScript engines, no additional installation needed to run simple code
- Choose a suitable text editor (Visual Studio Code recommended)
- Install Node.js if you need to run JavaScript on the server side
- Configure development tools and plugins to improve development efficiency
In the next chapter, we'll learn the basic syntax of JavaScript, which is an important first step to mastering this language.