Here are some key concepts of JavaScript that you can quickly revise:
-
Variables and Data Types:
- Use
var
,let
, orconst
to declare variables. - Data types: numbers, strings, booleans, null, undefined, objects, arrays.
- Use
-
Operators:
- Arithmetic operators (+, -, *, /, %).
- Comparison operators (>, <, >=, <=, ===, !==).
- Logical operators (&&, ||, !).
-
Conditional Statements:
if
,else if
,else
for decision making.- Ternary operator (
condition ? expr1 : expr2
).
-
Loops:
for
loop,while
loop,do-while
loop.break
andcontinue
statements.
-
Functions:
- Declare functions using
function
keyword or arrow functions. - Functions can have parameters and a return value.
- Declare functions using
-
Arrays:
- Create arrays using square brackets
[]
. - Access elements using index (0-based).
- Array methods like
push
,pop
,shift
,unshift
,slice
, etc.
- Create arrays using square brackets
-
Objects:
- Objects store key-value pairs.
- Access properties using dot notation or brackets.
- Use objects for complex data structures.
-
DOM Manipulation:
- Interact with HTML and CSS using JavaScript.
- Access and modify elements with
getElementById
,querySelector
, etc. - Change element content, attributes, and styles.
-
Events:
- Respond to user interactions using event listeners.
- Common events: click, submit, keyup, etc.
-
Scope and Closures:
- Understand global and local scope.
- Closures - functions that remember their lexical environment.
-
Asynchronous JavaScript:
- Use callbacks, promises, or async/await for async operations.
- Handle asynchronous tasks like fetching data or making API calls.
-
Modules:
- Organize code into reusable modules.
- Use
export
andimport
statements.
-
Error Handling:
- Handle errors with
try
,catch
,finally
.
- Handle errors with
-
JSON:
- JavaScript Object Notation for data interchange.
JSON.stringify()
andJSON.parse()
.
-
AJAX and Fetch API:
- Make HTTP requests to the server.
- Fetch data asynchronously.
-
ES6 Features:
- Arrow functions, template literals, destructuring, spread syntax, etc.
Remember, JavaScript is a vast language, and this is just a quick overview of its key concepts. For a more in-depth understanding, make sure to explore each topic further and practice writing code regularly. Happy coding!
#JavaScript concepts with examples:
Here's a concise overview of key JavaScript concepts with examples:
-
Variables and Data Types:
let age = 25; // Number let name = 'John'; // String let isStudent = true; // Boolean let fruits = ['apple', 'banana', 'orange']; // Array let person = { name: 'Alice', age: 30 }; // Object
-
Operators:
let x = 5; let y = 10; let sum = x + y; // Addition let product = x * y; // Multiplication let isGreater = x > y; // Comparison let isValid = true && false; // Logical AND
-
Conditional Statements:
let temperature = 30; if (temperature > 25) { console.log('It is hot outside!'); } else if (temperature > 15) { console.log('It is warm outside!'); } else { console.log('It is cold outside!'); }
-
Loops:
for (let i = 0; i < 5; i++) { console.log(i); // Prints 0 to 4 } let fruits = ['apple', 'banana', 'orange']; for (let fruit of fruits) { console.log(fruit); // Prints each fruit in the array }
-
Functions:
function add(a, b) { return a + b; } let result = add(3, 5); // result will be 8
-
Arrays:
let fruits = ['apple', 'banana', 'orange']; console.log(fruits[0]); // Access the first element (apple) fruits.push('grape'); // Add an element to the end
-
Objects:
let person = { name: 'Alice', age: 30 }; console.log(person.name); // Access property by dot notation console.log(person['age']); // Access property by bracket notation
-
DOM Manipulation:
<button id="myButton">Click Me</button>
let button = document.getElementById('myButton'); button.addEventListener('click', function () { console.log('Button clicked!'); });
-
Events:
<input type="text" id="myInput">
let input = document.getElementById('myInput'); input.addEventListener('keyup', function (event) { console.log('Key pressed: ' + event.key); });
-
Scope and Closures:
function outer() { let x = 10; function inner() { console.log(x); // Inner function can access x from the outer scope } return inner; } let closureFunc = outer(); closureFunc(); // Prints 10
-
Asynchronous JavaScript (using Promises):
function fetchData() { return new Promise((resolve, reject) => { setTimeout(() => { resolve('Data fetched successfully'); }, 2000); }); } fetchData().then((data) => { console.log(data); // Prints 'Data fetched successfully' after 2 seconds });
-
Modules:
// In math.js export function add(a, b) { return a + b; } // In main.js import { add } from './math.js'; let result = add(3, 5); // result will be 8
-
Error Handling:
try { // Code that might throw an error } catch (error) { console.log('An error occurred: ' + error.message); } finally { console.log('This block always executes.'); }
-
JSON:
let person = { name: 'John', age: 25 }; let jsonStr = JSON.stringify(person); // Convert object to JSON string console.log(jsonStr); // Prints '{"name":"John","age":25}' let personObj = JSON.parse(jsonStr); // Convert JSON string to object console.log(personObj.name); // Prints 'John'
-
AJAX and Fetch API:
fetch('https://api.example.com/data') .then((response) => response.json()) .then((data) => console.log(data)) .catch((error) => console.log('Error:', error));
-
ES6 Features:
// Arrow function let add = (a, b) => a + b; // Template literals let name = 'Alice'; console.log(`Hello, ${name}!`); // Prints 'Hello, Alice!' // Destructuring let { age } = person; console.log(age); // Prints 25 // Spread syntax let nums = [1, 2, 3]; let newNums = [...nums, 4, 5]; console.log(newNums); // Prints [1, 2, 3, 4, 5]
Remember to practice these concepts regularly to strengthen your understanding of JavaScript. Happy coding!