1

why I can log the imported variable but it show undefined when I added to another variable.

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module" src="index.js"></script>
</body>
</html>

index.js

import log from './log.js';
export var test = 'Hello';
log();

log.js

import { test } from './index.js';
var x = test + '  Word'
export default function log() {
    console.log(test);
    console.log(x);
}

console log

Hello
undefined  Word

1 Answer 1

1

In log.js you need to initialize var "x" in the function. This will work, I tested.

import { test } from './index.js';
export default function log() {
  var x = test + '  Word'
  console.log(test);
  console.log(x);
}
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.