Do we have an option to change/override the drupalsettings variable which is defined by some other module through our custom module? What is the best approach to change/override the drupalsettings variable which is defined by some other module (basically core) through our custom module ? If I follow the same hooks then how to call it at very last in Drupal 8?
Here: by drupalsettings, I am asking about the Drupal behaviors variable.
The question is updated as suggested by @leymannx.
You can alter the drupalSettings with the hook &quot;hook_js_settings_alter&quot;:
function hook_js_settings_alter(array &amp;$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {

  // Add settings.
  $settings['user']['uid'] = \Drupal::currentUser();

  // Manipulate settings.
  if (isset($settings['dialog'])) {
    $settings['dialog']['autoResize'] = FALSE;
  }
}

For more information see: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_js_settings_alter/8.9.x
I checked the code and the theme hook is run after the module hook. So if you really want to be last. Add the hook to your theme.
web/core/lib/Drupal/Core/Asset/AssetResolver.php:
$this-&gt;moduleHandler-&gt;alter('js_settings', $settings, $assets);
$this-&gt;themeManager-&gt;alter('js_settings', $settings, $assets);