Sitemap

Getting Started with JavaScript

Basic concepts and examples

--

Press enter or click to view image in full size

JavaScript is a popular programming language used for creating dynamic web pages and interactive user interfaces. If you’re just starting out with JavaScript, this guide will provide you with a solid foundation to get started.

Variables and Data Types

JavaScript uses variables to store data values. There are three ways to declare variables in JavaScript: using var, let, or const. The var keyword is used to declare a variable with global or function scope, while let and const are used for block scope.

var myVariable = "Hello, World!"; // global or function scope
let myNumber = 42; // block scope
const myConstant = true; // read-only variable

JavaScript supports several built-in data types, including strings, numbers, booleans, arrays, and objects.

let myString = "This is a string";
let myNumber = 42;
let myBoolean = true;
let myArray = [1, 2, 3];
let myObject = { name: "John", age: 30 };

Get Manoranjan Jena’s stories in your inbox

Join Medium for free to get updates from this writer.

Functions

Functions are reusable blocks of code that can be called from different parts of your program. In JavaScript, functions can be declared using the function keyword or using arrow functions.

// Function declaration
function addNumbers(num1, num2) {
return num1 + num2;
}

// Arrow function
const multiplyNumbers = (num1, num2) => {
return num1 * num2;
};

Control Flow Statements

JavaScript includes several control flow statements, including if/else statements, switch statements, for loops, while loops, and do/while loops. These statements allow you to control the flow of your code and execute certain code blocks conditionally or repeatedly.

// If/else statement
let myNumber = 42;
if (myNumber === 42) {
console.log("The answer to the ultimate question of life, the universe, and everything.");
} else {
console.log("I'm sorry, I do not have an answer.");
}

// For loop
for (let i = 0; i < 10; i++) {
console.log(i);
}

// While loop
let counter = 0;
while (counter < 10) {
console.log(counter);
counter++;
}

Objects and Classes

In JavaScript, objects are a fundamental concept that allows you to group related data and functions together. Objects can be created using object literals or using constructor functions.

// Object literal
const person = {
name: "John",
age: 30,
sayHello() {
console.log(`Hello, my name is ${this.name}`);
}
};

// Constructor function
function Person(name, age) {
this.name = name;
this.age = age;
}

Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};

const john = new Person("John", 30);
john.sayHello();

Conclusion

JavaScript is a versatile language that is used for building web applications and many other types of software. By mastering the basics of JavaScript, including variables, data types, functions, control flow statements, and objects, you’ll be well on your way to building your own powerful JavaScript applications.

Stay tuned for discovering more on JS in the coming blogs :)

--

--