Sharing JavaScript code between Lightning Web Components (LWC) can make your application more modular and maintainable. There are two primary ways to share code:

1. JavaScript Modules:

You can create reusable JavaScript modules to hold common utility functions or constants. These modules can be imported into any LWC component that needs them.

   Creating a Module:

   Let's say you have common utility functions you want to use across multiple components. You can        create a module named myUtils.js in a shared folder (not inside any LWC component folder).


Using the Module in a Component:
Import the shared utility function or constant in any LWC component that needs it.



2. Service Components:

These are LWC components without a user interface (i.e., no template). They serve as "services" by providing methods and properties that other components can utilize. This pattern is useful for more complex shared logic that might involve maintaining some state or using LWC lifecycle hooks.

Creating a Service Component:

In your LWC directory, you'd create a new component (with its folder) but might not have an HTML file for it. The JavaScript file would have methods and properties decorated with @api so that they can be accessed by other components.

Using the Service Component:

In the component that needs to utilize the service, you'd import the service component and access its methods or properties.


By using shared JavaScript modules or service components, you can reduce code duplication and make your LWC application more maintainable and scalable.