Skip to content

Instantly share code, notes, and snippets.

View rajat2502's full-sized avatar
🏠
Working from home

Rajat Verma rajat2502

🏠
Working from home
View GitHub Profile
import { currentUsers, tasks } from './export_with_alias.js'
// This will import the currentUsers and tasks arrays into the module
const users = ['Ajay', 'Akash', 'Raman'];
const tasks = ['write article', 'practice', 'read docs'];
export { users as currentUsers, tasks };
// This way the array 'users' will be exported as 'currentUsers'
// 1. exporting with the defination
export default function logUser() {
console.log(user);
}
/* OR */
// 2. Exporting at the end of the file
function logUser() {
console.log(user);
// 1. Named export
// a) Exporting one by one
const ids = [1, 2, 3, 4]; // Not availiable outside this file
export const users = ['Ajay', 'Akash', 'Raman'];
export const tasks = ['write article', 'practice', 'read docs'];
/* Here, we are exporting the variables one by one in their declaration
but, we can also export them in a single line as: */
const users = ['Ajay', 'Akash', 'Raman'];
const tasks = ['write article', 'practice', 'read docs'];
export { users, tasks };
export const users = ['Ajay', 'Akash', 'Raman'];
export const tasks = ['write article', 'practice', 'read docs'];
const ids = [1, 2, 3, 4]; // Not availiable outside this file
console.log('Hi'); // prints: Hi
setTimeout(function() {
console.log('There'); // prints: There
}, 0);
console.log('Peeps!'); // prints: Peeps!
// The callback function inside the setTimeout() gets executed at the end.
while(1) {
/* Warning: blocking code! */
};
const url = 'https://jsonplaceholder.typicode.com/posts';
fetch(url, {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1
}),
headers: {
const url = 'https://jsonplaceholder.typicode.com/todos/1';
fetch(url)
.then(response => response.json())
.then(json => {
console.log(json);
/* prints on success:
{
"userId": 1,
"id": 1,