0

I've seen 2 ways for importing modules in JavaScript and TypeScript respectively and I'm confused.

(1) Method 1: Import

const * = require('./runtime');

(1) Method 1: Export

exports.login = function() {};

(2) Method 2: Import

import * from './runtime';

(2) Method 2: Export

export function login() {};

May I know which one to use with in JavaScript and which one with TypeScript?

1
  • 2
    Both methods can be used in JavaScript and TypeScript. The first is called CommonJS and the second ES5 modules. Commented Nov 5, 2021 at 9:33

1 Answer 1

1

Both are correct however the first 2 ones are older and used in jquery or lower versions of javascript and One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. you can also refer here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.