JavaScript is one of the most widely used languages for web development. In this post, we'll cover the essential building blocks of JavaScript: data types, variables, and functions.
Data Types in JavaScript
JavaScript has several built-in data types. These are categorized into primitive and non-primitive (object) types.
Primitive Data Types
String – Text data
let name = "Alice";
Number – Integer or floating-point numbers
let age = 25;
Boolean – true or false
let isStudent = true;
Undefined – A variable that has been declared but not assigned a value
let x;
Null – Represents an intentional absence of value
let data = null;
Symbol – Unique and immutable primitive value
BigInt – For large integers
Non-Primitive (Objects, Arrays, Functions)
let person = { name: "Bob", age: 30 };
let numbers = [1, 2, 3];
Variables: var, let, and cons
*JavaScript uses keywords to declare variables:
*
var – Function-scoped (older way)
let – Block-scoped, can be reassigned
const – Block-scoped, cannot be reassigned
let city = "New York";
const country = "USA";
Functions in JavaScript
Functions are reusable blocks of code that perform specific tasks
Declaring a Function
function greet() {
console.log("Hello, world!");
}
Calling a Function
greet(); // Output: Hello, world!
Function Arguments
You can pass values to functions using arguments.
function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Alice"); // Output: Hello, Alice!
Return Values
Functions can return values using the return keyword.
function add(a, b) {
return a + b;
}
let sum = add(5, 10);
console.log(sum); // Output: 15
Top comments (0)