Getting Started with JavaScript
JavaScript is a versatile, high-level programming language that is widely supported
across all major operating systems.
You can run JavaScript on your computer using the following two methods:
Run JavaScript online
Run JavaScript on your computer
Run JavaScript Online
You can try our free online JavaScript compiler to run JavaScript code directly in your
browser without any need for installation.
Run JavaScript on Your Computer
To install JavaScript on your Windows, just follow these steps:
1. Install VS Code
2. Download Node msi file
3. Install Node
4. Verify your installation
Here is a detailed explanation of each of the steps:
Step 1: Install VS Code
Go to the VS Code Official website and download the Windows installer. Once the
download is complete, run the installer and follow the installation process.
Click Finish to complete the installation process
Step 2: Download the Node File
Go to the official Node website and download the Node LTS version.
Step 3: Run the Installer
Now, go to your download folder and run the msi file you just downloaded.
Depending on your security settings, you might be prompted to allow access. Just
click on Next.
Simply allow it and proceed.
Then you will come across the install part.
Simply click on install, Node will be successfully installed in your device.
Step 4: Verify your installation
After the installation is complete, you can verify whether JavaScript is installed by
using the following command in the command prompt.
node --version
Note: The version number might differ from the one above, depending on your
installed version.
Run Your First JavaScript Program
First open VS Code, click on the File in the top menu and then select New File.
Then, save this file with a .js extension by clicking on File again, then Save As, and
type your filename ending in .js. (Here, we are saving it as [Link]).
Before you start coding, make sure the extension Code Runner is installed in VS
Code. Open VS Code and click on Extensions on the left sidebar. Then, search for
the Code Runner and click on install.
Now, write the following code into your [Link] file:
[Link]("Hello World")
Then click on the run button on the top right side of your screen.
You should see Hello World! printed to the output.
JavaScript Variables and Constants
JavaScript Variables
A JavaScript variable is a container for storing data. For example, let num = 5;
Here, num is a variable that stores the number 5.
Declare Variables in JavaScript
In JavaScript, we use the var or let keywords to declare variables. For example,
Here, age and name are variables.
What is the difference between var and let?
Both var and let are used to declare variables. However, there are some differences
between them.
var let
let is the new way of declaring
var is used in older versions of
variables, starting with ES6
JavaScript.
(ES2015).
Variables created with var are Variables declared with let are
function-scoped, meaning they can block-scoped, meaning they can
be accessed anywhere within the only be accessed within the block
function they were defined in. where they were declared.
For example, var x; For example, let y;
Note: It is recommended we use let instead of var. However, there are a few
browsers that do not support let.
Initialize Variables in JavaScript
We use the assignment operator = to assign a value to a variable.
// declare variable num
let num;
// assign 5 to num
num = 5;
Here, 5 is assigned to the variable num.
You can also initialize variables during its declaration.
// declare variable num1 and assign 5 to it
let num1 = 5;
// declare variable num2 and assign 6 to it
let num2 = 6;
Declare multiple variables in a single statement
In JavaScript, it's possible to declare multiple variables in a single statement.
// declare variables num1, num2, and num3
// assign values 5, 6, and 7 respectively
let num1 = 5, num2 = 6, num3 = 7;
Here, we have declared and assigned values to three variables in a single line:
The value assigned to num1 is 5.
The value assigned to num2 is 6.
The value assigned to num3 is 7.
Use a variable without initializing it:
If you use a variable without initializing it, it will have an undefined value.
// declare variable num
let num;
// print num
[Link](num);
// Output: undefined
Here, we have declared a variable named num. However, since it does not contain
any value, its value is undefined.
Change the Value of Variables
The value of a variable may vary. Hence, the name variable.
Let's look at the example below to learn how to change the value of a variable:
// assign 5 to variable score
let score = 5;
[Link](score); // 5
// change the value of score to 3
score = 3;
[Link](score); // 3
Rules for Naming JavaScript Variables
Variable names must start with a letter, an underscore _, or the dollar sign $.
For example,
// valid
let message = "hello";
let _message = "hello";
let $message = "hello";
Variables cannot start with numbers. For example,
Variable names are case-sensitive. So age and Age are different variables. For
example,
let age = 23;
let Age = 20;
[Link](age); // 23
[Link](Age); // 20
Variable names cannot be keywords (special words reserved for specific
purposes in JavaScript such as if, else, let, var, etc.). For example,
//invalid
let new = 5; // Error! new is a keyword
Recommended ways to name a variable in JavaScript
You can name the variables any way you want. However, we recommend you use the
following naming conventions:
In JavaScript, variables are generally named in camelCase format if they have
multiple words.
For example, firstName, annualSalary, numberOfBooks, etc.
It's a good practice to give a descriptive name to a variable.
For example, if you are using a variable to store the number of apples, it is
better to name that variable apples or numberOfApples rather than x or n.
JavaScript Constants
[Link]