JavaScript Basics
Last Updated :
29 Mar, 2024
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:
oldest keywords to declare a variable and var can be updated and redeclared.
|
block-scoped, can’t be accessible out the particular block, and let can be updated but not redeclared
|
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);
{
let num = 20;
console.log(num)
}
console.log(num)
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
Represent both integer and floating-point numbers.
|
A string is a sequence of characters. In JavaScript, strings can be enclosed within single or double quotes.
|
Represent a logical entity and can have two values: true or false.
|
This type has only one value: null. It is left intentionally so that it shows something that does not exist.
|
A variable that has not been assigned a value is undefined.
|
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.
|
The bigint type represents the whole numbers that are larger than 253-1. To form a bigint literal number
|
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
console.log(323)
console.log(0b11);
const str1 = "GeeksforGeeks" ;
console.log(str1);
let student = true ;
console.log(student);
let val = null ;
console.log(val);
let undefinedValue;
console.log(undefinedValue);
let id = Symbol( "id" );
console.log(id);
let bigNumber = 123456789012345678901234567890n;
console.log(bigNumber);
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
executing code based on a condition.
|
Alternative code execution when ‘if’ condition is not met.
|
Additional condition check after ‘if’ and before ‘else’ 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));
|
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:
Executes code for a specific number of iterations.
|
Repeats code while a condition is true.
|
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);
}
|
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
single-line comments start with //.
|
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
console.log( "Hello Geeks!" );
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
These are the operators that operate upon the numerical values and return a numerical value.
|
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.
|
used to perform the logical operations that determine the equality or difference between the values
|
used to make decisions based on conditions specified for the statements.
|
JavaScript shorthand conditional operator for simple if-else statements.
|
performing bitwise operations on binary representations of numbers.
|
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
let a = 5 + 3;
console.log(a);
let b = 10 - 4;
console.log(b);
let y = 5;
y += 3;
console.log(y);
let z = 15;
z -= 6;
console.log(z);
|
We have a complete list of JavaScript Operators, to check those, please go through this JavaScript Operators Reference article.
Share your thoughts in the comments
Please Login to comment...