π My JavaScript Learning Journey β Day 1
Today, I started learning JavaScript, one of the most important languages for web development. In this blog, I will share what I learned today with simple explanations.
π‘ Why Do We Use JavaScript?
JavaScript is used to make websites interactive and dynamic. HTML gives structure, CSS gives style, but JavaScript adds life to the website.
For example, it helps in:
- Showing alert messages
- Creating sliders or animations
- Validating forms (like checking if email is correct)
- Building real-time updates without refreshing the page
π Why Are Functions Mostly Used in JavaScript?
A function is a block of code that performs a specific task.
It is used to avoid repeating code. Instead of writing the same code again and again, we write it once in a function and call it wherever needed.
Example:
function greet() {
console.log("Hello, welcome to my blog!");
}
This function will print a welcome message.
Letβs now learn how to call this function.
π£ How to Call a Function
To run (or call) a function, just write the function name followed by parentheses ()
.
Example:
greet(); // This will print: Hello, welcome to my blog!
So, whenever you write greet();
, it runs the code inside the function.
π€ Variables in JavaScript
Variables are containers for storing data.
You can create a variable using:
-
var
(older) -
let
(modern and recommended) -
const
(for values that should not change)
Example:
let name = "John";
const age = 25;
var city = "Mumbai";
π§ͺ Types of Variables
Keyword | Scope | Can Reassign | Description |
---|---|---|---|
var | Function | Yes | Older and not recommended much |
let | Block | Yes | Used when value can change |
const | Block | No | Used when value should stay same |
π JavaScript Data Types
JavaScript has two main types of data:
β Primitive Data Types:
-
String β
"Hello"
-
Number β
42
,3.14
-
Boolean β
true
orfalse
- Null β No value
- Undefined β Not assigned yet
- Symbol β Unique values (advanced)
- BigInt β Large numbers
π§± Non-Primitive Data Types:
-
Object β
{name: "John", age: 25}
-
Array β
["apple", "banana"]
- Function β Reusable block of code
β Why Do We Use Data Types?
Data types help the browser understand what kind of data we are using.
For example:
- A number is used for calculations.
- A string is used for text.
- A boolean is used for conditions (true or false).
This helps JavaScript work smoothly and avoid errors.
Top comments (0)