Introduction to JavaScript: Getting started with basic syntax and functions

JavaScript is a powerful programming language that is extensively used in web development. It is a high-level, dynamic, and interpreted language that is easy to learn, making it an ideal language for beginners. In this blog, you’ll learn a comprehensive introduction to JavaScript, covering the basic syntax and functions that you need to get started.

Getting Started with JavaScript

To get started with JavaScript, you need to have an understanding of fundamentals like HTML and CSS. JavaScript is a cross-platform, scripting language that is used to add interactivity and functionality to web pages or apps. It is executed on the client side as well as the server side, which means that it runs on the user's computer and the server (The computer that stores and delivers website content).

JavaScript is included in every modern web browser, which means that you don't need to install any software to get started. You can start by opening a text editor such as Notepad, Sublime Text, or Visual Studio Code and writing your first JavaScript program.

Basic Syntax of JavaScript

JavaScript has a similar syntax to other programming languages such as Java and C++. It is case-sensitive, which means that 'hello' and 'Hello' are two different variables.

Here's an example of a JavaScript program that shows a message in an alert box:


// variable declaration
let message;

// Assigning a value to the variable
message = "Hello, World!";

// Using an alert box for displaying a message
alert(message);

In this program, you can declare a variable named 'message' using the 'let' keyword. We then assign a string value of "Hello, World!" to the variable. Finally, use the 'alert' function to display the value of the 'message' variable in an alert box.

Functions in JavaScript

Functions are an essential part of JavaScript. They are blocks of code that perform a specific task and can be reused throughout your program. Here's an example of a basic function that adds the two numbers and displays the result in alert box:


// Defining a function that adds two numbers
function addNumbers(num1, num2) {
    return num1 + num2;
}

// Calling the function and storing the result in the variable
let result = addNumbers(9, 10);

// Showing the result in an alert box
alert(result);

Here, you can define a function named 'addNumbers' that takes two parameters, 'num1' and 'num2', and returns their sum. furthermore, you can call the 'addNumbers' function with arguments of 9 and 10 and store the result in a variable named 'result'. Finally, using the 'alert' function displays the value of the 'result' variable in an alert box.

Variables and Data Types in JavaScript

Variables are used to store data in JavaScript. They can store a variety of data types, including strings, numbers, and boolean.

  • String

A string is a data type that is used to represent a sequence of characters, such as text or words. it can be enclosed in single quotes (' ') or double quotes (" "). For example, "Hello, Techie!" and 'JavaScript is awesome!' are both strings.

  • Number

A number is a data type that is used to represent numeric values. JavaScript supports both integers and floating-point numbers. For example, 5 and 3.14 are both numbers.

  • Boolean

Boolean is a data type that is used to represent logical values, either true or false. Boolean values are often used in conditional statements and comparison operations. For example, the expression 5 > 3 evaluates to true, while the expression 5 < 3 evaluates to false.

// Declaring different data types

//string variable
let name = "John Doe";

// Number variable
let age = 25;

//Boolean variable
let isStudent = true;

In the above program, we declare a string variable named 'name' that stores the value "John Doe". We also declare a number variable named 'age' that stores the value 25, and a boolean variable named 'isStudent' that stores the value true.

Conditional Statements in JavaScript

Conditional Statements allow the execution of different blocks of code depending on whether a condition is true or false. They can be used to validate user input, perform calculations based on user input, and much more.

The most frequently used conditional statement is the if-else statement. It allows you to execute one block of code if a condition is true and another block of code if the condition is false.

You can use an if-else statement as follows:


if (condition) {
  // this block is executed if the condition is true
} else {
  // this block is executed if the condition is false
}

Here, the condition is a boolean expression that evaluates to true or false. If the condition is true, the code in the first block is executed. If the condition is false, the code in the second block is executed.

For example, you want to write a program that checks if a number is positive or negative. if-else statement is written as follows:


let num = 5;

if (num > 0) 
  console.log("This is a positive number");
} else {
  console.log("This is a negative number");
}

In this example, if num is greater than 0, the program will print "This is a positive number". Otherwise, it will print "This is a negative number".

Loops in JavaScript

Loops allow to execute the same block of code multiple times, making it easier to perform repetitive tasks.

Mainly there are three types of loop: for loop, while loop, and do while loop. JavaScript's most common loop type is the for loop. It allows you to iterate over a sequence of values and perform a specific action on each item.

For loops are written as follows:


for (initialization; condition; increment/decrement) {
  // The code to run for each iteration
}

Here, initialization is an expression that sets the initial value of the loop variable, the condition is a boolean expression that is evaluated at the beginning of each iteration to determine if the loop should continue, and increment/decrement is an expression that updates the loop variable at the end of each iteration.

For example, if you want to write a program that calculates the sum of the first 10 natural numbers. You use a for loop for the same:


let sum = 0;

for (let i = 1; i <= 10; i++) {
  sum += i;
}

console.log("The sum is " + sum);

In this example, the loop variable i is initialized to 1, and the loop continues as long as i is less than or equal to 10. On each iteration, the value of i is added to the sum variable. After the loop is complete, the program prints "The sum is 55".

Conclusion

To sum up, JavaScript is a powerful language that is widely used in web development. With its simple syntax and dynamic functionality, JavaScript has become an essential part of modern web development. In this blog post, we have covered some of the basic syntax and functions of JavaScript, including variables, functions, conditional statements, and loops. By understanding these building blocks, you can start to create your own JavaScript programs and explore the endless possibilities of this versatile language by learning more through documentation.

Whether you're a beginner or an experienced developer, JavaScript provides endless opportunities to create dynamic and interactive web applications. So, keep learning and exploring this amazing language.

Learned something? If yes, would appreciate a like so more like-minded people can gain insights!

For technical blogs, Feel free to connect on Twitter or LinkedIn.