JavaScript Basics Introduction
Learn the fundamentals of JavaScript, including variables, data types, operators, conditions, loops, and functions. JavaScript is an essential programming language for web development and Node.js applications.
1. What Is JavaScript?
JavaScript is a programming language used to create interactive and dynamic applications. It can run in web browsers and on servers through environments such as Node.js.
JavaScript allows developers to perform calculations, validate forms, handle events, manipulate web pages, communicate with APIs, and build server-side applications.
Common uses of JavaScript:
- Creating interactive web pages.
- Developing server-side applications using Node.js.
- Building web APIs and backend services.
- Validating user input and processing data.
- Developing frontend applications using JavaScript frameworks.
2. How JavaScript Works
A JavaScript program contains instructions. A compatible JavaScript runtime executes those instructions and produces results.
JavaScript Code
Variables, expressions, functions, and statements
JavaScript Runtime
Browser or Node.js
Program Execution
Output, calculations, or application behavior
Note: This is a simplified learning diagram. The actual execution process depends on the JavaScript runtime and the program.
3. Your First JavaScript Program
Let us start with a simple program that displays a message in the
terminal using console.log().
Step 1: Create a File
Create a file named hello.js.
Step 2: Write the Code
console.log("Hello, JavaScript!");
console.log("Welcome to CIIT Training Institute.");
Step 3: Run the Program
node hello.js
Step 4: Expected Output
Hello, JavaScript!
Welcome to CIIT Training Institute.
Explanation:
The console.log() function displays the supplied
values in the terminal. Each call prints its message on a new line.
4. Variables in JavaScript
Variables are used to store values that a program can access
and process. JavaScript provides three commonly used declarations:
var, let, and const.
| Keyword | Description | Reassignment |
|---|---|---|
var |
Function-scoped or globally scoped declaration. | Allowed. |
let |
Block-scoped variable declaration. | Allowed. |
const |
Block-scoped binding that must be initialized. | Not allowed for the binding. |
Example
let studentName = "Samadhan";
let age = 22;
const instituteName = "CIIT Training Institute";
console.log(studentName);
console.log(age);
console.log(instituteName);
Output
Samadhan
22
CIIT Training Institute
Explanation:
The variables store a student name, age, and institute name.
We use console.log() to display their values.
5. JavaScript Data Types
A data type describes the kind of value stored or processed by a program. JavaScript includes primitive types and objects.
| Data Type | Example | Description |
|---|---|---|
| String | "Hello" |
Text values. |
| Number | 100 |
Numeric values, including integers and decimals. |
| Boolean | true |
Logical true or false values. |
| Undefined | undefined |
A value indicating that a variable has not been assigned a value. |
| Null | null |
An intentional absence of a value. |
| BigInt | 123n |
Integers larger than the safe integer range of Number can represent precisely. |
| Symbol | Symbol("id") |
A unique symbol value. |
| Object | { "{ name: 'Samadhan' }" } |
Stores related properties and values. Arrays and functions are also objects. |
Example
let name = "Samadhan";
let age = 22;
let isStudent = true;
let result;
console.log(typeof name);
console.log(typeof age);
console.log(typeof isStudent);
console.log(typeof result);
Output
string
number
boolean
undefined
6. JavaScript Operators
Operators perform operations on values and variables. They are used in calculations, comparisons, and logical expressions.
| Operator Type | Examples | Purpose |
|---|---|---|
| Arithmetic | + - * / % |
Performs mathematical operations. |
| Comparison | === !== > < |
Compares values. |
| Logical | && || ! |
Combines or reverses logical conditions. |
| Assignment | = += -= |
Assigns or updates values. |
Example
let a = 10;
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a % b);
console.log(a > b);
console.log(a === b);
Output
13
7
30
1
true
false
7. Conditions in JavaScript
Conditional statements execute different blocks of code depending on whether a condition is true or false.
Example: if-else
let marks = 75;
if (marks >= 40) {
console.log("Student passed");
} else {
console.log("Student failed");
}
Output
Student passed
Explanation:
Since marks is 75, the condition
marks >= 40 is true. Therefore, the first block executes.
8. Loops in JavaScript
Loops execute a block of code repeatedly while a condition is satisfied or while items remain to be processed.
Example: for Loop
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Output
1
2
3
4
5
The loop starts with i = 1, continues while
i <= 5, and increases i by one
after each iteration.
9. Functions in JavaScript
A function is a reusable block of code that performs a particular task. Functions can accept parameters and return values.
Example
function add(a, b) {
return a + b;
}
let result = add(10, 20);
console.log("Sum:", result);
Output
Sum: 30
Explanation: The function receives two parameters, adds their values, and returns the result. The returned value is then displayed.
10. Practice Task
Student Result Program
Create a JavaScript program that:
- Stores a student's name and marks in variables.
- Displays the student's name and marks.
- Uses an if-else statement to check whether the student passed.
- Uses a loop to display numbers from 1 to 5.
- Creates a function that calculates the total of two numbers.
Important Points
- JavaScript is used in browsers and Node.js applications.
- Use
letfor reassigned variables andconstfor bindings that should not be reassigned. - JavaScript supports primitive data types and objects.
- Operators perform calculations and comparisons.
- Conditional statements control program flow.
- Loops repeat instructions.
- Functions make code reusable and organized.
Summary
JavaScript is a programming language used to build interactive web applications and server-side programs with Node.js. In this introduction, we learned about variables, data types, operators, conditions, loops, and functions. These fundamentals help us understand more advanced JavaScript concepts and develop practical applications.