JavaScript is a powerful and flexible programming language used to create interactive websites and web applications. Two of the core concepts every developer must understand are data types and functions. Let’s dive into each of these.
---
JavaScript Data Types
In JavaScript, data types represent different kinds of values. They are mainly divided into two categories:
- Primitive Data Types These are the basic building blocks:
| Type | Example | Description |
|---|---|---|
String |
"Hello" |
Text enclosed in quotes |
Number |
42, 3.14
|
Integers and floating-point numbers |
Boolean |
true, false
|
Logical values |
Null |
null |
Empty or non-existent value |
Undefined |
undefined |
Variable declared but not assigned |
Symbol |
Symbol("id") |
Unique and immutable value |
BigInt |
1234567890123456789n |
Large integers beyond Number limits |
---
- Non-Primitive (Reference) Data Types These can hold multiple values or complex structures:
•Object: { name: "John", age: 30 }
•Array: [1, 2, 3]
•Function: function() { ... }
let name = "Alice"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let score = null; // Null
let address; // Undefined
let user = { name: "Bob", city: "NYC" }; // Object
🔧 JavaScript Functions
A function is a block of code designed to perform a particular task.
Declaring a Function
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Output: Hello, Alice!
Function Expressions
const add = function (a, b) {
return a + b;
};
console.log(add(2, 3)); // Output: 5
Arrow Functions (ES6+)
const multiply = (x, y) => x * y;
console.log(multiply(4, 5)); // Output: 20
Why Use Functions?
Code reusability
Better organization
Easier to test and debug
📝 Conclusion
Understanding data types and functions is fundamental in JavaScript. Mastering these basics lays the foundation for building dynamic and efficient applications.
Top comments (0)