All JavaScript syntax
All JavaScript syntax and their functions, how to use them and when to apply them
Avatar
JavaScript is a versatile programming language with a wide range of syntax and
functions. It's impossible to cover every single aspect in one response, but I can
provide an overview of some key syntax elements, their functions, how to use them,
and when to apply them. Let's break it down into several categories:
1. Variables and Data Types
Variables
var: Declares a variable with function scope.
JavaScript
Copy
var x = 10;
Usage: Use var when you need a variable that is scoped to the function it is declared
in.
When to use: Rarely used in modern JavaScript due to its function scope and hoisting
behavior.
let: Declares a variable with block scope.
JavaScript
Copy
let y = 20;
Usage: Use let when you need a variable that is scoped to the block it is declared in.
When to use: Preferred over var for most use cases.
const: Declares a constant with block scope.
JavaScript
Copy
const z = 30;
Usage: Use const when you need a value that should not be reassigned.
When to use: Use for constants and when you want to ensure a value is not
reassigned.
Data Types
Primitive Types: String, Number, Boolean, Undefined, Null, Symbol, BigInt.
JavaScript
Copy
let name = "John"; // String
let age = 25; // Number
let isApproved = true; // Boolean
let x; // Undefined
let y = null; // Null
let id = Symbol('id'); // Symbol
let bigNumber = BigInt(12345678901234567890123456789012345678901234567890n); //
BigInt
Usage: Use these types to store different kinds of data.
When to use: Choose the appropriate type based on the data you need to store.
Complex Types: Object, Array, Function.
JavaScript
Copy
let person = { name: "John", age: 25 }; // Object
let numbers = [1, 2, 3, 4, 5]; // Array
let greet = function() { console.log("Hello!"); }; // Function
Usage: Use these types to store collections of data or to define behavior.
When to use: Use objects for key-value pairs, arrays for ordered collections, and
functions for reusable code.
2. Control Structures
Conditional Statements
if/else:
JavaScript
Copy
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
Usage: Use to execute code based on a condition.
When to use: When you need to make decisions based on conditions.
switch:
JavaScript
Copy
switch (day) {
case 'Monday':
console.log("It's Monday.");
break;
case 'Tuesday':
console.log("It's Tuesday.");
break;
default:
console.log("It's another day.");
}
Usage: Use to execute code based on multiple conditions.
When to use: When you have multiple possible values for a variable and want to
handle each case differently.
Loops
for:
JavaScript
Copy
for (let i = 0; i < 5; i++) {
console.log(i);
Usage: Use to repeat a block of code a specific number of times.
When to use: When you know the number of iterations in advance.
while:
JavaScript
Copy
let i = 0;
while (i < 5) {
console.log(i);
i++;
Usage: Use to repeat a block of code while a condition is true.
When to use: When the number of iterations is not known in advance.
do...while:
JavaScript
Copy
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
Usage: Use to repeat a block of code at least once and then while a condition is true.
When to use: When you want to ensure the code runs at least once.
3. Functions
Function Declaration
JavaScript
Copy
function greet(name) {
console.log("Hello, " + name + "!");
greet("John");
Usage: Use to define a reusable block of code.
When to use: When you need to perform a task multiple times.
Function Expression
JavaScript
Copy
let greet = function(name) {
console.log("Hello, " + name + "!");
};
greet("John");
Usage: Use to define a function and assign it to a variable.
When to use: When you need to use a function as a value or pass it as an argument.
Arrow Functions
JavaScript
Copy
let greet = (name) => {
console.log("Hello, " + name + "!");
};
greet("John");
Usage: Use for concise function syntax.
When to use: When you want a shorter syntax for functions, especially for callbacks
and inline functions.
4. Arrays and Objects
Arrays
Accessing Elements:
JavaScript
Copy
let numbers = [1, 2, 3, 4, 5];
console.log(numbers[0]); // 1
Usage: Use to access elements by index.
When to use: When you need to retrieve or modify elements in an array.
Array Methods:
JavaScript
Copy
numbers.push(6); // Add element to the end
numbers.pop(); // Remove element from the end
numbers.unshift(0); // Add element to the beginning
numbers.shift(); // Remove element from the beginning
Usage: Use to manipulate arrays.
When to use: When you need to add, remove, or modify elements in an array.
Objects
Accessing Properties:
JavaScript
Copy
let person = { name: "John", age: 25 };
console.log(person.name); // John
console.log(person["age"]); // 25
Usage: Use to access properties by key.
When to use: When you need to retrieve or modify properties in an object.
Object Methods:
JavaScript
Copy
person.greet = function() {
console.log("Hello, my name is " + this.name);
};
person.greet(); // Hello, my name is John
Usage: Use to add methods to objects.
When to use: When you need to define behavior for an object.
5. ES6+ Features
Template Literals
JavaScript
Copy
let name = "John";
let age = 25;
console.log(`Hello, my name is ${name} and I am ${age} years old.`);
Usage: Use to create strings with embedded expressions.
When to use: When you need to include variables or expressions in strings.
Destructuring
JavaScript
Copy
let person = { name: "John", age: 25 };
let { name, age } = person;
console.log(name, age); // John 25
Usage: Use to extract values from objects or arrays.
When to use: When you need to access multiple properties or elements at once.
Spread Operator
JavaScript
Copy
let numbers = [1, 2, 3];
let moreNumbers = [...numbers, 4, 5];
console.log(moreNumbers); // [1, 2, 3, 4, 5]
Usage: Use to spread elements of an array or properties of an object.
When to use: When you need to combine arrays or objects.
6. Asynchronous Programming
Callbacks
JavaScript
Copy
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
fetchData((data) => {
console.log(data);
});
Usage: Use to handle asynchronous operations.
When to use: When you need to perform an action after an asynchronous task
completes.
Promises
JavaScript
Copy
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data loaded");
}, 1000);
});
fetchData().then(data => {
console.log(data);
});
Usage: Use to handle asynchronous operations more cleanly.
Copy
Try Again
Share
Continue
Asynchronous Programming
Callbacks
JavaScript
Copy
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
fetchData((data) => {
console.log(data);
});
Usage: Use to handle asynchronous operations.
When to use: When you need to perform an action after an asynchronous task
completes.
Promises
JavaScript
Copy
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data loaded");
}, 1000);
});
fetchData().then(data => {
console.log(data);
});
Usage: Use to handle asynchronous operations more cleanly.
When to use: When you need to chain asynchronous operations or handle errors
more gracefully.
Async/Await
JavaScript
Copy
async function fetchData() {
return "Data loaded";
async function displayData() {
let data = await fetchData();
console.log(data);
displayData();
Usage: Use to write asynchronous code that looks synchronous.
When to use: When you need to make asynchronous code more readable and
maintainable.
Conclusion
JavaScript is a powerful language with a rich set of syntax and functions.
Understanding when to use each feature can greatly enhance your ability to write
efficient and maintainable code. This overview covers some of the most common
elements, but there's always more to learn as JavaScript continues to evolve.