All JavaScript-WPS Office
All JavaScript-WPS Office
All JavaScript syntax and their functions, how to use them and when to apply them
Avatar
Variables
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.
JavaScript
Copy
let y = 20;
Usage: Use let when you need a variable that is scoped to the block it is declared in.
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
JavaScript
Copy
let x; // Undefined
When to use: Choose the appropriate type based on the data you need to store.
JavaScript
Copy
2. Control Structures
Conditional Statements
if/else:
JavaScript
Copy
} else {
switch:
JavaScript
Copy
switch (day) {
case 'Monday':
console.log("It's Monday.");
break;
case 'Tuesday':
console.log("It's Tuesday.");
break;
default:
When to use: When you have multiple possible values for a variable and want to
handle each case differently.
Loops
for:
JavaScript
Copy
console.log(i);
while:
JavaScript
Copy
let i = 0;
while (i < 5) {
console.log(i);
i++;
do...while:
JavaScript
Copy
let i = 0;
do {
console.log(i);
i++;
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) {
greet("John");
Function Expression
JavaScript
Copy
};
greet("John");
Arrow Functions
JavaScript
Copy
};
greet("John");
When to use: When you want a shorter syntax for functions, especially for callbacks
and inline functions.
Arrays
Accessing Elements:
JavaScript
Copy
console.log(numbers[0]); // 1
Array Methods:
JavaScript
Copy
When to use: When you need to add, remove, or modify elements in an array.
Objects
Accessing Properties:
JavaScript
Copy
console.log(person.name); // John
console.log(person["age"]); // 25
Object Methods:
JavaScript
Copy
person.greet = function() {
};
5. ES6+ Features
Template Literals
JavaScript
Copy
let name = "John";
Destructuring
JavaScript
Copy
When to use: When you need to access multiple properties or elements at once.
Spread Operator
JavaScript
Copy
console.log(moreNumbers); // [1, 2, 3, 4, 5]
6. Asynchronous Programming
Callbacks
JavaScript
Copy
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
fetchData((data) => {
console.log(data);
});
When to use: When you need to perform an action after an asynchronous task
completes.
Promises
JavaScript
Copy
function fetchData() {
setTimeout(() => {
resolve("Data loaded");
}, 1000);
});
fetchData().then(data => {
console.log(data);
});
Copy
Try Again
Share
Continue
Asynchronous Programming
Callbacks
JavaScript
Copy
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
fetchData((data) => {
console.log(data);
});
When to use: When you need to perform an action after an asynchronous task
completes.
Promises
JavaScript
Copy
function fetchData() {
setTimeout(() => {
resolve("Data loaded");
}, 1000);
});
fetchData().then(data => {
console.log(data);
});
When to use: When you need to chain asynchronous operations or handle errors
more gracefully.
Async/Await
JavaScript
Copy
console.log(data);
displayData();
When to use: When you need to make asynchronous code more readable and
maintainable.
Conclusion