2

I have declared const variable like,

"student.js"
 export default const mark= 20;

I am Calling this constant in index.js file

"index.js"
import {mark} from './student';
console.log("Mark Value ::::" + mark);

am getting error????

4
  • 2
    am getting error???? You are asking or telling? Commented Feb 15, 2018 at 7:26
  • import mark from './student'; Commented Feb 15, 2018 at 7:26
  • it wont work . am asking the reason. Commented Feb 15, 2018 at 7:27
  • 1
    export default means you are exporting a module. export const means you are exporting part of a module and module will be formed later after combining all exports. So you should get error on export default const itself Commented Feb 15, 2018 at 7:29

3 Answers 3

9

export default expects an expression. While const is a statement.

You can't do export default const mark = 20 for the same reason you can't do console.log(const mark = 20).

If mark isn't used anywhere else in this file, it should be:

export default 20;

Otherwise it should be:

const mark = 20;
export default mark;

And imported like:

import mark from './student';
Sign up to request clarification or add additional context in comments.

Comments

1

Adding to @estus answer, for your code to work change as follows.

"student.js"

export const mark = 20;

"index.js"

import {mark} from './student';
console.log("Mark Value ::::" + mark);

1 Comment

Thanks for looking this question. I will try like this way.:)
0

What happens on export default var a = 10, b = 20, c = 30?

Check out this discussion.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.