Skip to the content.

JavaScript Cheatsheet

Table Of Contents

JavaScript Description

JavaScript is a high-level, interpreted programming language that is used to create interactive and dynamic web pages. It was first introduced in 1995 by Netscape and has since become one of the most popular programming languages in the world.

The purpose of JavaScript is to add interactivity and functionality to web pages. It allows developers to create dynamic effects, animations, and user interfaces that can respond to user actions in real-time. JavaScript can also be used for server-side programming, mobile app development, and game development.

JavaScript is used extensively in web development for creating interactive websites, web applications, and mobile apps. It is also used for creating browser extensions, desktop applications, and game development. Many popular websites such as Facebook, Twitter, Gmail, and Google Maps use JavaScript extensively to provide a rich user experience.

Overall, JavaScript has become an essential tool for modern web development due to its versatility and ability to create dynamic and interactive web pages.

Generated by ChatGPT

Node Commands

Command Description Reference
$ npm i --package-lock-only Update package-lock.json Link
$ npm install <package> Add the <package> dependency to the project  
$ npm run <script> Run the script named <script>  
$ npm install Install all dependencies  

HTTP Request

The fetch() method is used to make HTTP requests in JavaScript. It is a modern replacement for XMLHttpRequest (XHR) and provides a simpler and more flexible API.

Here’s how to use fetch() to make an HTTP POST request:

fetch('https://example.com/data', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'John Doe',
    email: 'johndoe@example.com'
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

In this example, we’re making a POST request to https://example.com/data with some JSON data in the body. We’re setting the Content-Type header to application/json and using JSON.stringify() to convert our data object into a JSON string. We’re then parsing the response as JSON and logging it to the console.

Note that fetch() returns a Promise, which allows us to chain multiple methods together using .then(). We can also catch errors using .catch().

Generated By ChatGPT

Redirect

fetch(url, { method: 'POST' })
    .then(response => {
        if (response.redirected) {
            window.location.href = response.url;
        }
    })

Access Cookies

js-cookie: A simple, lightweight JavaScript API for handling browser cookies

Create a cookie, valid across the entire site:

Cookies.set('name', 'value')

Create a cookie that expires 7 days from now, valid across the entire site:

Cookies.set('name', 'value', { expires: 7 })

Create an expiring cookie, valid to the path of the current page:

Cookies.set('name', 'value', { expires: 7, path: '' })

Read cookie:

Cookies.get('name') // => 'value'
Cookies.get('nothing') // => undefined

Read all visible cookies:

Cookies.get() // => { name: 'value' }

Handwrite Notes

# Key Description
1 iterate on list for of iterate on list elements - for-in iterate on object key
2 iterate on list for in iterate on range of list length
3 iterate on object for in iterate on object keys
4 defer This attribute will load code after HTML