{"id":24570,"date":"2024-01-15T23:43:39","date_gmt":"2024-01-15T22:43:39","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/como-crear-y-utilizar-modulos-en-javascript-para-un-codigo-mas-organizado\/"},"modified":"2024-06-03T17:36:25","modified_gmt":"2024-06-03T16:36:25","slug":"como-crear-y-utilizar-modulos-en-javascript-para-un-codigo-mas-organizado","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/","title":{"rendered":"How to Create and Use Modules in JavaScript for More Organized Code"},"content":{"rendered":"<p>JavaScript is a programming language that has grown exponentially in popularity and usage over the last decade. As web applications become increasingly complex, the need to keep code organized and manageable becomes a priority. This is where JavaScript modules come in, which are essential for the organization of JS code. In this article we will delve into how to create and use modules in JavaScript to better structure our code and make it more maintainable and reusable.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#%C2%BFQue_son_los_Modulos_JavaScript\" >What are JavaScript Modules?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Beneficios_de_Utilizar_Modulos\" >Benefits of Using Modules<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Asi_Puedes_Crear_Modulos_en_JavaScript\" >This is how you can create modules in JavaScript<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Exportaciones_Nombradas\" >Named Exports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Exportacion_por_Defecto\" >Default Export<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Importar_Modulos_en_Tu_Aplicacion\" >Import Modules into Your Application<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Importando_Exportaciones_Nombradas\" >Importing Named Exports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Importando_una_Exportacion_por_Defecto\" >Importing a Default Export<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Importaciones_Combinadas\" >Combined Imports<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Buenas_Practicas_para_el_Uso_de_Modulos\" >Good Practices for Using Modules<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Estructura_de_Directorios_Logica\" >Logical Directory Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Nomenclatura_Consistente\" >Consistent Nomenclature<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Importaciones_Especificas\" >Specific Imports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Documentacion\" >Documentation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Pruebas_Unitarias\" >Unit tests<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-create-and-use-modules-in-javascript-for-a-more-organized-code\/#Consideraciones_Finales\" >Final Considerations<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_son_los_Modulos_JavaScript\"><\/span>What are JavaScript Modules?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JavaScript modules are individual files or pieces of code that can export functions, objects, or variables to be used in other parts of the application. The main advantage of using modules is that they allow the code to be divided into smaller logical blocks that can be developed, tested and debugged independently.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Beneficios_de_Utilizar_Modulos\"><\/span>Benefits of Using Modules<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Maintainability<\/strong>: Facilitates code maintenance by being divided into smaller, more manageable pieces.<\/li>\n<li><strong>Reuse<\/strong>: Allows you to reuse the same code in different parts of the application or even between different projects.<\/li>\n<li><strong>Namespace<\/strong>: Avoid name conflicts when encapsulating variables and functions.<\/li>\n<li><strong>Dependency Management<\/strong>: Clarifies dependencies between different parts of the code.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Asi_Puedes_Crear_Modulos_en_JavaScript\"><\/span>This is how you can create modules in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To create a module in JavaScript, we simply must define the functions, classes or variables that we want to export and use the keyword <code>export<\/code>. There are two main types of export: named and default.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Exportaciones_Nombradas\"><\/span>Named Exports<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This type of exports allows several elements to be exported from the same module. The syntax is used <code>export<\/code> followed by the element to export.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ file mathUtils.js export const add = (a, b) =&gt; a + b; export const subtract = (a, b) =&gt; a - b;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Exportacion_por_Defecto\"><\/span>Default Export<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In some cases, a module may want to export only one element, or a parent element. In this case, we can use the default export.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ calculator.js file export default class Calculator { constructor(initialValue = 0) { this.value = initialValue; } add(value) { this.value += value; } \/\/ ... other methods }<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Importar_Modulos_en_Tu_Aplicacion\"><\/span>Import Modules into Your Application<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once we have created our modules, it is time to use them in other parts of our application. To import elements of a module, we use the keyword <code>import<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Importando_Exportaciones_Nombradas\"><\/span>Importing Named Exports<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We can import the named exports individually or all at once using the syntax <code>import<\/code>.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ file main.js import { add, subtract } from &#039;.\/mathUtils.js&#039;; console.log(sum(5, 3)); \/\/ 8 console.log(subtract(5, 3)); \/\/ 2<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Importando_una_Exportacion_por_Defecto\"><\/span>Importing a Default Export<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To import the default export of a module, it is not necessary to use curly braces in the statement <code>import<\/code>.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ file main.js import Calculator from &#039;.\/calculator.js&#039;; const myCalculator = new Calculator(); myCalculator.add(5);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Importaciones_Combinadas\"><\/span>Combined Imports<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It is possible to combine default and named imports if the module offers both types of export.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">import Calculator, { add, subtract } from &#039;.\/mathUtils.js&#039;;<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Buenas_Practicas_para_el_Uso_de_Modulos\"><\/span>Good Practices for Using Modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are some tips that will improve the organization of JS code using modules:<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Estructura_de_Directorios_Logica\"><\/span>Logical Directory Structure<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Organize your modules in a directory structure that reflects your application&#039;s domain or modules&#039; functionality.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Nomenclatura_Consistente\"><\/span>Consistent Nomenclature<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Use consistent nomenclature for both module file names and identifiers you export.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Importaciones_Especificas\"><\/span>Specific Imports<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Import only what you need. This reduces the possibility of conflicts and unnecessary code loading.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Documentacion\"><\/span>Documentation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Document what each module exports and what it is for. Comments and documentation improve the understandability of the modules.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Pruebas_Unitarias\"><\/span>Unit tests<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Each module must have its own set of unit tests to ensure that each piece works correctly in isolation.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Consideraciones_Finales\"><\/span>Final Considerations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The use of modules in JavaScript is key to maintaining clean, organized and scalable code. With practice, modularization becomes an essential skill for any front-end or full-stack developer. Furthermore, modern frameworks and libraries such as React, Angular and Vue are module-intensive, reinforcing their importance in the contemporary web development ecosystem.<\/p>\n<p>Creating and using modules is not only a good practice in itself, but it also future-proofs your code by making it easier to collaborate, maintain, and extend. By adopting this modular way of working, you will not only be writing better code but also contributing to the efficiency and success of your projects. <\/p>\n<p>Always remember to experiment and look for the most efficient ways to modularize your JavaScript projects, ensuring that the benefits of maintainability, reusability, and clarity are always present. With these concepts and techniques at your disposal, you are ready to develop large JavaScript applications with well-organized, easy-to-manage code.<\/p>","protected":false},"excerpt":{"rendered":"<p>JavaScript es un lenguaje de programaci\u00f3n que ha crecido exponencialmente en popularidad y uso en la \u00faltima d\u00e9cada. A medida que las aplicaciones web se vuelven cada vez m\u00e1s complejas, la necesidad de mantener el c\u00f3digo organizado y manejable se convierte en una prioridad. Aqu\u00ed es donde entran los m\u00f3dulos JavaScript, que son fundamentales para [&hellip;]<\/p>","protected":false},"author":1,"featured_media":24571,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420],"tags":[38,205,525,340,172,661,185,57,1693,626,1428,60,179],"class_list":["post-24570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-como","tag-blog","tag-codigo","tag-crear","tag-del","tag-estructura","tag-javascript","tag-mas","tag-modularidad","tag-modulos","tag-organizado","tag-para","tag-utilizar"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/24570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=24570"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/24570\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/24571"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=24570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=24570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=24570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}