Skip to main content

Java Script

 Java Script Cheat Sheet

Here's a cheat sheet summarizing some of the common JavaScript concepts, functions, and syntax:

javascript
// JavaScript Cheat Sheet // Variables let variableName = value; // Declare a variable with block scope var variableName = value; // Declare a variable with function scope (deprecated) const constantName = value; // Declare a constant variable // Data Types let number = 10; // Number let string = "Hello"; // String let boolean = true; // Boolean let array = [1, 2, 3]; // Array let object = { // Object key1: value1, key2: value2 }; // Operators let sum = num1 + num2; // Addition let difference = num1 - num2; // Subtraction let product = num1 * num2; // Multiplication let quotient = num1 / num2; // Division let remainder = num1 % num2; // Modulus let isGreater = num1 > num2; // Greater than let isEqual = num1 === num2; // Equality let logicalAnd = condition1 && condition2; // Logical AND let logicalOr = condition1 || condition2; // Logical OR let logicalNot = !condition; // Logical NOT // Conditionals if (condition) { // Code executed if condition is true } else if (condition2) { // Code executed if condition2 is true } else { // Code executed if no previous conditions are true } // Loops for (let i = 0; i < limit; i++) { // Code executed in each iteration } while (condition) { // Code executed repeatedly while condition is true } do { // Code executed at least once, then repeatedly while condition is true } while (condition); // Functions function functionName(parameter1, parameter2) { // Code executed by the function return value; // Optional return statement } // Arrays let array = [1, 2, 3]; // Array declaration let length = array.length; // Length of the array let element = array[index]; // Accessing an element array.push(value); // Add an element to the end array.pop(); // Remove the last element array.unshift(value); // Add an element to the beginning array.shift(); // Remove the first element array.splice(index, count); // Remove or replace elements // Objects let object = { // Object declaration key1: value1, key2: value2, method: function() { // Method code } }; let value = object.key1; // Accessing a value object.key3 = value3; // Adding a new key-value pair delete object.key2; // Deleting a key-value pair // Events element.addEventListener('event', function() { // Code executed when the event occurs }); // DOM Manipulation let element = document.getElementById('id'); // Get element by ID let elements = document.getElementsByClassName('class'); // Get elements by class name let elements = document.getElementsByTagName('tag'); // Get elements by tag name element.innerHTML = 'Text'; // Set element's HTML content element.value = 'Value'; // Set element's value element.style.property = 'value'; // Set element's CSS style element.classList.add('class'); // Add a class to an element element.classList.remove('class'); // Remove a class from an element // Promises (Asynchronous) new Promise(function(resolve, reject) { // Asynchronous code if (success) { resolve(result); } else { reject(error); } }).then(function(result) { // Code executed on successful promise }).catch(function(error) { // Code executed on rejected promise }); // Fetch API (HTTP Requests) fetch(url) .then(function(response) { return response.json(); }) .then(function(data) { // Process the response data }) .catch(function(error) { // Handle errors }); // Modules (ES6) // Exporting module export const variableName = value; export function functionName() { // Code } // Importing module import { variableName, functionName } from './module.js';

This cheat sheet covers some of the commonly used JavaScript concepts, functions, syntax, conditionals, loops, arrays, objects, events, DOM manipulation, promises, Fetch API, and modules. It serves as a handy reference for quick look-ups and reminders while working with JavaScript.

Comments

Popular posts from this blog

Web Programming: HTML, DHTML, XML, Scripting, Java, Servlets, Applets

 Web programming encompasses various technologies and concepts used to develop web applications. Let's explore each of them in detail: 1. HTML (Hypertext Markup Language): HTML is the standard markup language used to create the structure and content of web pages. It uses tags to define elements like headings, paragraphs, images, links, forms, etc. Example: ```html <!DOCTYPE html> <html> <head>     <title>My Web Page</title> </head> <body>     <h1>Hello, World!</h1>     <p>This is a paragraph.</p>     <img src="image.jpg" alt="Image">     <a href="https://www.example.com">Visit Example</a> </body> </html> ``` 2. DHTML (Dynamic HTML): DHTML is a combination of HTML, CSS, and JavaScript that allows web pages to become more dynamic and interactive. Example (DHTML with JavaScript): ```html <!DOCTYPE html> <htm...

Variable Naming Rule

 When naming variables in the C programming language, it's important to follow certain rules and conventions to ensure clarity, readability, and maintainability of your code. Here are the general rules for variable naming in C: 1. Valid Characters:    - Variable names can consist of letters (both uppercase and lowercase), digits, and the underscore (_) character.    - The first character of a variable name must be a letter or an underscore. 2. Length Limitation:    - C does not impose a specific limit on the length of variable names, but it is recommended to keep them reasonably short and descriptive. 3. Case Sensitivity:    - C is a case-sensitive language, so variable names with different cases are treated as different variables.    - For example, "count" and "Count" are considered distinct variables. 4. Reserved Keywords:    - You cannot use C reserved keywords as variable names since they have predefined meanings in th...

Classes and Objects in python with Example

  # Classes and Objects class ClassName : def __init__ ( self, parameter1, parameter2 ): self.parameter1 = parameter1 self.parameter2 = parameter2 def method_name ( self ): # Code executed by the method object_name = ClassName(parameter1, parameter2) # Create an object object_name.method_name() # Access object's methods   In Python, classes and objects are the foundation of object-oriented programming (OOP). A class is a blueprint for creating objects, while an object is an instance of a class. Classes define attributes (variables) and methods (functions) that represent the characteristics and behaviors of objects. Let's explore some examples to understand classes and objects in Python. Example 1: Creating a simple class and object: ```python class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): print(f"Car: {self.year...