In JavaScript, you can declare variables using var
, let
, and const
.
var
has function - level scope.let
and const
have block - level scope. const
is used for constants, whose value cannot be reassigned.// Using var
var age = 25;
// Using let
let name = 'John';
// Using const
const PI = 3.14159;
JavaScript has primitive data types (number, string, boolean, null, undefined, symbol) and reference data types (objects, arrays, functions).
// Number
let num = 10;
// String
let message = 'Hello, World!';
// Boolean
let isStudent = true;
// Null
let emptyValue = null;
// Undefined
let notDefined;
// Object
let person = {
name: 'Jane',
age: 30
};
// Array
let numbers = [1, 2, 3, 4, 5];
function add(a, b) {
return a + b;
}
let result = add(3, 5);
console.log(result); // Output: 8
const multiply = function(a, b) {
return a * b;
};
let product = multiply(4, 6);
console.log(product); // Output: 24
const divide = (a, b) => a / b;
let quotient = divide(10, 2);
console.log(quotient); // Output: 5
let temperature = 22;
if (temperature > 20) {
console.log('It is warm outside.');
} else {
console.log('It is cool outside.');
}
let day = 3;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
break;
default:
console.log('Invalid day');
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
let j = 0;
while (j < 3) {
console.log(j);
j++;
}
let car = {
make: 'Toyota',
model: 'Corolla',
year: 2020,
start: function() {
console.log('The car has started.');
}
};
console.log(car.make); // Output: Toyota
car.start(); // Output: The car has started.
let fruits = ['apple', 'banana', 'cherry'];
fruits.push('date');
console.log(fruits); // Output: ['apple', 'banana', 'cherry', 'date']
function outerFunction() {
let outerVariable = 'I am from the outer function';
function innerFunction() {
console.log(outerVariable);
}
innerFunction();
}
outerFunction(); // Output: I am from the outer function
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
let double = multiplier(2);
console.log(double(5)); // Output: 10
function fetchData(callback) {
setTimeout(() => {
let data = 'Some data from the server';
callback(data);
}, 2000);
}
fetchData((result) => {
console.log(result);
});
function getData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
let data = 'Promise data';
resolve(data);
}, 1500);
});
}
getData().then((result) => {
console.log(result);
});
async function main() {
try {
let data = await getData();
console.log(data);
} catch (error) {
console.error(error);
}
}
main();
'use strict';
) at the beginning of your JavaScript files to catch common coding mistakes.JavaScript is a rich and powerful programming language with a wide range of features. By understanding its key concepts such as variables, functions, control structures, objects, arrays, scope, closures, and asynchronous programming, you can write more efficient and maintainable code. Adopting common practices and best practices will further enhance the quality of your JavaScript projects, whether you are working on the front - end or back - end.