Variables are used to store data. In JavaScript, you declare variables using var
, let
, or const
.
let name = "John"; // Variable that can be reassigned
const age = 30; // Constant variable that cannot be reassigned
var isStudent = true; // Older way to declare variables, function-scoped
JavaScript has different data types, including numbers, strings, booleans, and arrays.
let number = 42; // Number
let string = "Hello World"; // String
let isActive = false; // Boolean
let numbers = [1, 2, 3]; // Array
Operators are used to perform operations on values and variables.
let sum = 10 + 5; // Arithmetic operator
let isEqual = (10 === 10); // Comparison operator
let isTrue = (true && false); // Logical operator
Functions allow you to reuse blocks of code.
function greet(name) {
return "Hello, " + name;
}
// Function Call
let message = greet("John"); // "Hello, John"
Write a function sum
that finds the sum of two numbers.