Skip to content

atharva-0123-cs/Basic-JavaScrpit

Repository files navigation

Basic-JavaScrpit

Here are some key concepts of JavaScript that you can quickly revise:

  1. Variables and Data Types:

    • Use var, let, or const to declare variables.
    • Data types: numbers, strings, booleans, null, undefined, objects, arrays.
  2. Operators:

    • Arithmetic operators (+, -, *, /, %).
    • Comparison operators (>, <, >=, <=, ===, !==).
    • Logical operators (&&, ||, !).
  3. Conditional Statements:

    • if, else if, else for decision making.
    • Ternary operator (condition ? expr1 : expr2).
  4. Loops:

    • for loop, while loop, do-while loop.
    • break and continue statements.
  5. Functions:

    • Declare functions using function keyword or arrow functions.
    • Functions can have parameters and a return value.
  6. Arrays:

    • Create arrays using square brackets [].
    • Access elements using index (0-based).
    • Array methods like push, pop, shift, unshift, slice, etc.
  7. Objects:

    • Objects store key-value pairs.
    • Access properties using dot notation or brackets.
    • Use objects for complex data structures.
  8. DOM Manipulation:

    • Interact with HTML and CSS using JavaScript.
    • Access and modify elements with getElementById, querySelector, etc.
    • Change element content, attributes, and styles.
  9. Events:

    • Respond to user interactions using event listeners.
    • Common events: click, submit, keyup, etc.
  10. Scope and Closures:

    • Understand global and local scope.
    • Closures - functions that remember their lexical environment.
  11. Asynchronous JavaScript:

    • Use callbacks, promises, or async/await for async operations.
    • Handle asynchronous tasks like fetching data or making API calls.
  12. Modules:

    • Organize code into reusable modules.
    • Use export and import statements.
  13. Error Handling:

    • Handle errors with try, catch, finally.
  14. JSON:

    • JavaScript Object Notation for data interchange.
    • JSON.stringify() and JSON.parse().
  15. AJAX and Fetch API:

    • Make HTTP requests to the server.
    • Fetch data asynchronously.
  16. 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:

  1. 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
  2. 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
  3. 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!');
    }
  4. 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
    }
  5. Functions:

    function add(a, b) {
      return a + b;
    }
    
    let result = add(3, 5); // result will be 8
  6. Arrays:

    let fruits = ['apple', 'banana', 'orange'];
    console.log(fruits[0]); // Access the first element (apple)
    fruits.push('grape'); // Add an element to the end
  7. 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
  8. DOM Manipulation:

    <button id="myButton">Click Me</button>
    let button = document.getElementById('myButton');
    button.addEventListener('click', function () {
      console.log('Button clicked!');
    });
  9. Events:

    <input type="text" id="myInput">
    let input = document.getElementById('myInput');
    input.addEventListener('keyup', function (event) {
      console.log('Key pressed: ' + event.key);
    });
  10. 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
  11. 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
    });
  12. 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
  13. 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.');
    }
  14. 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'
  15. 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));
  16. 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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published