EXPORT vs EXPORT DEFAULT – JavaScript interview questions
When working with JavaScript and specifically with the React framework, you may come across the terms EXPORT and EXPORT DEFAULT. These are used to export variables, functions, or objects from a module, but they have some differences that are important to understand.
EXPORT
The EXPORT keyword is used to export multiple variables, functions, or objects from a module. When using EXPORT, you can export multiple items from the same module by defining each item and then using the EXPORT keyword to make them available for import in other files.
// In file module.js
export const name = 'John';
export function sayHello() {
console.log('Hello!')
}
EXPORT DEFAULT
The EXPORT DEFAULT keyword is used to export a single variable, function, or object from a module. When using EXPORT DEFAULT, you can only export one item from the module, and when importing it in another file, you can use any name to refer to it.
// In file module.js
const name = 'Jane';
function sayHi() {
console.log('Hi!')
}
export default name;
When to use each?
When you have multiple items to export from a module, you should use the EXPORT keyword. This allows you to explicitly define what is being exported, and when importing it in another file, you need to use the same names as defined in the EXPORT statements.
On the other hand, when you only have one thing to export from a module, using EXPORT DEFAULT can be more convenient. You can use any name when importing it in another file, as it will always refer to the single item that was exported.
Conclusion
Understanding the differences between EXPORT and EXPORT DEFAULT is important when working with JavaScript modules, especially in the context of frameworks like React. Knowing when to use each can help you write cleaner and more maintainable code.
Course of JavaScript : https://bit.ly/3u049tf
Course of Modern JS. (ES6) : https://bit.ly/3DvYCh6
React JS MasterClass (10 Hour) : https://youtu.be/6l8RWV8D-Yo
NODE+ EXPRESS+ MONGO MasterClass (12 Hour) : https://youtu.be/ChVE-JbtYbM
React Interviews : https://bit.ly/3QAjAln
JavaScript Shorts:
http://bit.ly/3XhHRQ1
React Shorts :
http://bit.ly/3VfIrMi
Commenting here so that yt algo shows me real content and not random tiktok videos
Can you please tell which editor you are using, it shows output on select of function or variable, is it extension of any code editor?
isko module hi bolte hai na
Ye konsa code editor h , output instant dhik rha h , ?
Sir mene ek code me dekha tha ki import ../../components/navbar isko import @components/navbar ese kiya tha kese possible he?
Thank you coderdost it matters 👍👍
How to get realtime output?? Like showing in your VScode
👍
Plz tell me which one ide you are using??
Gr8
Keep it up
U will get high
Which code editor you use because direct output come in screen????
Ye toh pata tha but acha video h
Hi Good Evening sir. I am making my own Css library.
I am stuck that, How to create link ,which we put in our html to link our library like bootsap and tailwind
Which editor you are using
Next je pe kuch video Banao(shorts vi chalega) . Bohot problem ho raha hai . Internet pe detailed resource nahi milraha hai
Can u make a detailed video on export and import
How are you getting answer on the right side??
Bhai app kon sa text editor use karte ho
One more thing in multi import you can type “as” keyword to reassign them the new variable.
Import {add as abc, diff as ghi } from “./whatever”;