Open In App

JavaScript Basics

Last Updated : 29 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript is a versatile, lightweight, client-side scripting language used in web development. It can be used for both Client-side as well as Server-side developments. JavaScript is also known as a scripting language for web pages, It supports variables, data types, operators, conditional statements, loops, functions, arrays, and objects. With JavaScript, you can create dynamic, interactive, and engaging websites.

These are the basics that you need to learn before deep dive into JavaScript:

Variables in JavaScript

JavaScript Variables are the building blocks of any programming language. In JavaScript, variables can be used to store reusable values.

List of variables:

Variables name

Description

Var

oldest keywords to declare a variable and var can be updated and redeclared.

let

block-scoped, can’t be accessible out the particular block, and let can be updated but not redeclared

const

block scope, neither be updated nor redeclared.

Syntax:

var geek = "Hello Geek"         // Declaration using var
let $ = "Welcome" // Declaration using let
const _example = "Gfg" // Declaration using const

Example: Here is the basic example of using var, let, and const variables.

Javascript




var a = "Hello Geeks"
var b = 10;
  
console.log(a);
console.log(b);
  
// Using let
{
    let num = 20;
  
    // Calling the function inside block
    console.log(num)
}
  
// Calling the function outside 
// block throws a Error
console.log(num)
  
// Using const
const x = 22;
{
    const x = 90;
    console.log(x);
    {
        const x = 45;
        console.log(x);
    }
}
console.log(x);


Output:

10
Hello Geeks
20
Uncaught ReferenceError: num is not defined
90
45
22

Data types in JavaScript

Data type in JavaScript defines the type of data stored in a variable.

List of data types in javascript

Name

Description

Numbers

Represent both integer and floating-point numbers.

String

A string is a sequence of characters. In JavaScript, strings can be enclosed within single or double quotes.

Boolean

Represent a logical entity and can have two values: true or false.

Null

This type has only one value: null. It is left intentionally so that it shows something that does not exist.

Undefined

A variable that has not been assigned a value is undefined.

Symbol

Unlike other primitive data types, it does not have any literal form. It is a built-in object whose constructor returns a symbol that is unique.

bigint

The bigint type represents the whole numbers that are larger than 253-1. To form a bigint literal number

Object

It is the most important data-type and forms the building blocks for modern JavaScript.

Example: In this example, we are using the above-mentioned data types.

Javascript




// Decimal Numbers
console.log(323)
  
// Binary Numbers
console.log(0b11);
  
// String
const str1 = "GeeksforGeeks";
console.log(str1);
  
// Boolean
let student = true;
console.log(student);
  
// null
let val = null;
console.log(val);
  
// undefined
let undefinedValue;
console.log(undefinedValue);
  
// symbol
let id = Symbol("id");
console.log(id);
  
// Bigint
let bigNumber = 123456789012345678901234567890n;
console.log(bigNumber);
  
// Object
let person = {
    name: 'rahul',
    age: 21,
};
console.log(person.name);


Output

323
3
GeeksforGeeks
true
null
undefined
Symbol(id)
123456789012345678901234567890n
rahul

Conditional statements in javascript

Name

Description

if statement

executing code based on a condition.

else statement

Alternative code execution when ‘if’ condition is not met.

else if statement

Additional condition check after ‘if’ and before ‘else’ statement.

switch statement

Multi-case conditional statement for executing code based on variable’s value.

Syntax:

if (condition) {
// statement
} else if (condition) {
// statement
} else {
// statement
}

Example: Here is the basic example of using if statement and an else statement and an else if statement.

Javascript




const num = 0;
  
if (num > 0) {
    console.log("Given number is positive.");
} else if (num < 0) {
    console.log("Given number is negative.");
} else {
    console.log("Given number is zero.");
};


Output

Given number is zero.

Function in Javascript

A function in JavaScript is a block of reusable code that performs a specific task, can take inputs (parameters), and returns a result using the return statement.

Syntax:

function calcAddition(arg) {
...
}

Example: In this example, we are performing the addition of two numbers with the help of the javascript function.

Javascript




function calcAddition(number1, number2) {
    return number1 + number2;
}
console.log(calcAddition(6, 9));


Output

15

Looping in javascript

Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

List of loops in javascript:

Name

Description

for loop

Executes code for a specific number of iterations.

while loop

Repeats code while a condition is true.

do while loop

Repeats code, ensuring at least one execution.

Syntax:

for (statement 1 ; statement 2 ; statement 3){
code here...
}

Example: Here is the basic example of for loop.

Javascript




for (let i = 0; i < 5; i++) {
    console.log(i);
}


Output

0
1
2
3
4

Comments in javascript

JavaScript comments are used to explain the code to make it more readable. It can be used to prevent the execution of a section of code if necessary.

List of comments

Name

Description

Single line Comment

single-line comments start with //.

Multi line Comment

multi-line comments start with /* and end with */.

Syntax:

// A single line comment
/* It is multi line comment.
It will not be displayed upon
execution of this code
*/

Example: In this example, we are using single-line comments and multiline comments.

Javascript




// A single line comment
console.log("Hello Geeks!");
  
/* It is multi line comment.
It will not be displayed upon
execution of this code */
  
console.log("Multiline comment in javascript");


Output

Hello Geeks!
Multiline comment in javascript

Operators in javascript

JavaScript operators operate the operands, these are symbols that are used to manipulate a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands.

List of operators

Name

Description

Arithmetic Operators

These are the operators that operate upon the numerical values and return a numerical value.

Assignment Operators

assignment operator is equal (=) which assigns the value of the right-hand operand to its left-hand operand. That is if a = b assigns the value of b to a.

Comparison Operators

used to perform the logical operations that determine the equality or difference between the values

Logical Operators

used to make decisions based on conditions specified for the statements.

Ternary Operators

JavaScript shorthand conditional operator for simple if-else statements.

Bitwise Operators

performing bitwise operations on binary representations of numbers.

typeof Operator

returns the data type of its operand in the form of a string.

Example: Here are some basic examples of the above-mentioned operator.

Javascript




// Addition
let a = 5 + 3;
console.log(a);
  
// Subtraction
let b = 10 - 4;
console.log(b); 
  
// Addition Assignment
let y = 5;
y += 3;
console.log(y);
  
// Subtraction Assignment
let z = 15;
z -= 6;
console.log(z);


Output

8
6
8
9

We have a complete list of JavaScript Operators, to check those, please go through this JavaScript Operators Reference article.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads