What is JS File – JavaScript Tutorial for Beginners

What is JS File – JavaScript Tutorial for Beginners

What is JS File? This is a question that many people have when learning javascript tutorial for beginners. To begin with, a JS file is simply a file that contains JavaScript code. In this blog, we will discuss the basics of javascript tutorial for beginners and how to write your first JS program!

About Javascript

JavaScript is a programming language that was created in 1995 by Brendan Eich. It is a versatile language that can be used to create web applications, games, and more. JavaScript code is written in text files with the .js extension. These files are then linked to HTML pages to execute the code on a web page.

First JavaScript Program

JavaScript tutorial for beginners, to write your first JS program, you will need a text editor. You can use any text editor, but we recommend using a code editor such as Visual Studio Code or Sublime Text. Once you have your text editor set up, create a new file with the .js extension and open it in your code editor.

In your JS file, you can write your code. To start, let’s print out a message to the console. In JavaScript, we use the console.log() function to print out messages to the console.

console.log("Hello, world!");

Save your file and open it in a web browser. You should see the message "Hello, world!" printed in the browser console.

Congratulations, you’ve written your first JS program! Moving further, we’ll learn more about variables, data types, arrays, functions, loops, and conditional statements in JavaScript. Keep reading!

You can think of a variable as a simple storage container that stores an assigned value that can or cannot be changed during a program based on the keyword used when declaring. In JavaScript, variables can be declared with the "const" & "let" keywords. For example:

const x = 5; let randomNumber = 0.909082198;
Code language: JavaScript (javascript)

With the above example, you can observe that we have declared a variable named "x" with the value of "5" using the "const" the keyword which means that this value for "x" will remain unchanged throughout the lifecycle of the program, while the second example which uses a "let" keyword the value may or may not change while running the program. Variables can hold any type of data, including numbers, strings, and objects.

Data Types in JavaScript

JavaScript has various data types that can be used to store data. The data types can be divided into two main categories: primitive data types and object data types.

Primitive data types include Boolean, String, Number, and Null. These data types are immutable, meaning they cannot be changed.

Object data types include Array, Function, and Object. These data types are mutable, meaning they can be changed.

In addition to the data types mentioned above, JavaScript also has a data type called undefined. Undefined is used to indicate that a variable has not been assigned a value.

Array in JavaScript

An array in JavaScript is a data structure that stores a collection of items. Arrays are used to store data in a structured way and can be accessed using numerical indices. Items in an array can be of any data type, including strings, numbers, objects, and even other arrays. Arrays are a fundamental part of many programming languages and are often used to store data such as lists of items or to represent records with multiple fields.

JavaScript arrays are dynamic, meaning they can grow or shrink in size as needed. They are also mutable, meaning that their contents can be changed at any time. Arrays can be created using the Array constructor function, or by simply using square brackets.

let myArray = []; myArray = ['one', 'two', 'three']; console.log(myArray); // logs ['one', 'two', 'three']
Code language: JavaScript (javascript)

Arrays have several methods and properties that can be used to manipulate their contents. The most commonly used methods are push(), pop(), shift(), and unshift().

myArray.push('four'); // adds 'four' to the end of the array console.log(myArray); // logs ['one', 'two', 'three', 'four'] myArray.pop(); // removes the last element from the array console.log(myArray); // logs ['one', 'two', 'three'] myArray.shift(); // removes the first element from the array console.log(myArray); // logs ['two', 'three'] myArray.unshift('zero'); // adds 'zero' to the beginning of the array console.log(myArray); // logs ['zero', 'two', 'three']
Code language: Bash (bash)

Arrays also have several other methods that can be used to manipulate their contents, including sort(), reverse(), splice(), and concat(). For more information on these methods, see the MDN reference for Array.prototype.

Function in JavaScript

A function in JavaScript is a code block that performs a specific task. It can take one or more arguments and can return a value. Functions are defined using the keyword “function”, followed by a name, and parentheses (). The code to be executed by the function is written inside curly braces {}.

Functions can be called from anywhere in your code and can be used to perform tasks such as calculation, data processing, and more. In addition, functions can be assigned to variables and can be passed as arguments to other functions.

Functions can also be created anonymously – that is, without a name. Anonymous functions are often used when a function is only needed for a short period, or when the function is only used once.

The code inside a function is not executed until the function is called. This is known as “lazy execution”, and can be useful for optimizing code performance.

Functions can accept arguments – that is, values that are passed into the function when it is called. Arguments are separated by commas and can be of any data type.

Here are a few simple function examples:

function myFunction() { alert('Hello world!'); }
Code language: JavaScript (javascript)

You can call it in your code like this:


This will display an alert popup with the message 'Hello world!'.

You can also pass arguments into a function:

function myFunction(message) { alert(message); }
Code language: JavaScript (javascript)

And then call it like this:

myFunction('Hello world!');

This will also display an alert popup with the message ‘Hello world!’. You can put anything you want in the parentheses when you call the function – just make sure it matches what you’ve written in the function definition.

Loops in JavaScript

A loop is a repeating code block in JavaScript that lets you iterate through arrays or lists of data. The most common type of loop is the for loop, which allows you to repeat a code block a set number of times. Here is a look at how to use loops in JavaScript, with some examples to help you get started.

Loops are a fundamental part of programming, and they can be used in a variety of ways. In JavaScript, there are two main types of loops: for & while.

The for loop is the most common type of loop, and it lets you repeat a code block a set number of times. The syntax for a for loop is:

for (initialization; condition; increment) { // code block to be executed }
Code language: JavaScript (javascript)

The initialization statement is executed once before the loop starts. This is where you define the starting point for your loop. The condition statement is checked before each iteration of the loop, and if it evaluates to true, the code block is executed. If the condition statement evaluates to false, the loop will stop executing. The increment statement is executed after each iteration of the loop. This is where you define how the loop will change after each iteration.

The while loop is similar to the for loop, but it does not take in a condition statement. The code block will execute until the condition statement is no longer true.

while (condition) { // code block to be executed }
Code language: JavaScript (javascript)

Conditionals in JavaScript

A conditional statement is a set of commands that are executed only if a certain condition is true. In JavaScript, there are three different types of conditional statements: the if statement, the else statement, and the else if statement.

The if statement is the most basic type of conditional statement. It simply checks to see if a condition is true and if it is, the code inside the if statement is executed. On the other hand, if the condition is false, the code inside the if statement is skipped.

Here’s a simple example of an if statement:

if (condition) { // code to be executed if the condition is true }
Code language: JavaScript (javascript)

The else statement is used together with the if statement. It provides an alternative set of commands to be executed if the condition in the if statement is false.

Here’s an example of an if/else statement:

if (condition) { // code to be executed if the condition is true else { // code to be executed if the condition is false }
Code language: JavaScript (javascript)

You can also use the else if statement to check for multiple conditions. The following example checks for three different conditions:

if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else if (condition3) { // code to be executed if condition3 is true } else { // code to be executed if all conditions are false }
Code language: JavaScript (javascript)


Thanks for reading! I hope this has been helpful and it can help you get started with your programming journey using javascript tutorial for beginners.

If you enjoyed this blog, please share it with your friends! And if you’re new to coding, be sure to check out our other Interactive javascript tutorial for beginners courses on codedamn.

If you have any questions or comments, please feel free to reach out to me. I’m always happy to help in any way I can.

Again, thanks for reading!

Happy coding! 🙂

Sharing is caring

Did you like what Mridul Anand Singh wrote? Thank them for their work by sharing it on social media.


No comments so far