JavaScript is one of the most important programming languages used in web development.
It helps make websites interactive and dynamic.
we will understand:
- What is JavaScript
- Data Types in JavaScript
- Difference between Primitive and Non-Primitive
- Variables in JavaScript
1. What is JavaScript?
JavaScript is a versatile, dynamically typed programming language that brings life to web pages by making them interactive. It is used for building interactive web applications, supports both client-side and server-side development, and integrates seamlessly with HTML, CSS, and a rich standard library.
JavaScript is a single-threaded language that executes one task at a time.
It is an interpreted language which means it executes the code line by line.
The data type of the variable is decided at run-time in JavaScript, which is why it is called dynamically typed.
Example
<html>
<head></head>
<body>
<h1>Check the console for the message!</h1>
<script>
console.log("Hello, World!");
</script>
</body>
</html>
Output:
Hello World
2. Data Types in JavaScript
What is a Data Type?
A data type defines what kind of value a variable holds (number, text, etc.).
JavaScript is dynamically typed, meaning you donβt need to define the type manually.
Types of Data in JavaScript
According to W3Schools, JavaScript has 8 data types:
Primitive Data Types (7 types)
1. Number
let x = 10;
2. String
let name = "Athithya";
3. Boolean
let isStudent = true;
4. Undefined
let a;
5. Null
let value = null;
6. BigInt
let big = 12345678901234567890n;
7. Symbol
let id = Symbol("id");
These are called primitive because they store a single value.
Non-Primitive Data Type
Object
let person = {
name: "Athithya",
age: 21
};
Objects can store multiple values (key-value pairs).
Difference Between Primitive and Non-Primitive
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Type | Basic data | Complex data |
| Storage | Single value | Multiple values |
| Mutability | Immutable | Mutable |
| Examples | Number, String | Object, Array |
| Memory | Stored directly | Stored by reference |
Example
// Primitive
let a = 10;
let b = a;
b = 20;
console.log(a); // 10
// Non-Primitive
let obj1 = {name: "A"};
let obj2 = obj1;
obj2.name = "B";
console.log(obj1.name); // B
Primitive β value is copied
Non-Primitive β reference is copied
3. Variables in JavaScript
What is a Variable?
A variable is a container used to store data values.
Ways to Declare Variables
1. var
var x = 10;
- Old method
- Function scoped
2. let
let y = 20;
- Block scoped
- Can be updated
3. const
const z = 30;
- Cannot be reassigned
- Must be initialized
Example
let name = "Athithya";
let age = 21;
const country = "India";
console.log(name, age, country);
Output:
Athithya 21 India
typeof Operator
You can check data type using:
let x = "Hello";
console.log(typeof x);
Output:
string
The typeof operator returns the type of a variable.
Top comments (0)