By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Assign multiple const variables in single declaration
JavaScript

Javascript – Assign multiple const variables in single declaration

Admin
Last updated: 2024/01/07 at 6:51 AM
Admin
Share
2 Min Read
Assign multiple const variables in single declaration

Problem:

I’m rather new to react/JS therefore this question my sound somehow dumb:

Contents
Problem:Solution:

I need multiple variables that are set depending on a location(System variable). Those variables should be constants as they should never change for a given instance.

This is my current solution, it works but it feels very wrong to define variables inside other variable declarations. It also seems to not be possible to declare footerWidth and footerAltText as constants without an initial value.

 <Box
    as='img'
    width={footerWidth}
    justifyContent='center'
    alignContent='center'
    src={footerLogoSource}
    alt={footerAltText}
 />

Those attributes are provided by my features.ts:

export let footerWidth: string
export let footerAltText: string
export const footerLogoSource: string =
  getAppConfig().contextPath +
  (() => {
    switch (getAppConfig().landesvariante) {
      case Variante.SN:
        footerWidth = '30%'
        footerAltText = 'lorem ipsum'
        return '/assets/kofin_eu_logo.svg'
      case Variante.BB:
        footerWidth = '20%'
        footerAltText = 'lorem ipsum 2'
        return '/assets/logo_bb.svg'
      default:
        return null
    }
  })()

One solution would be to make 3 switch-case configurations for the 3 variables, but that would lead to alot of duplicated code…

Is there a proper way to do this?

Solution:

Here’s an approach using array destructuring

const [footerWidth, footerAltText, footerLogoSource] = (() => {
  const config = getAppConfig();

  switch (config.landesvariante) {
    case Variante.SN:
      return ['30%', 'lorem ipsum', `${config.contextPath}/assets/kofin_eu_logo.svg`];
    case Variante.BB:
      return ['20%', 'lorem ipsum 2', `${config.contextPath}/assets/logo_bb.svg`];
    default:
      return [undefined, undefined, null];
  }
})();

You can use object destructuring as well

const { width: footerWidth, altText: footerAltText, logo: footerLogoSource } = (() => {
  const config = getAppConfig();
  switch (config.landesvariante) {
    case Variante.SN:
      return {
        width: '30%',
        altText: 'lorem ipsum',
        logo: `${config.contextPath}/assets/kofin_eu_logo.svg`
      };
    case Variante.BB:
      return {
        width: '20%',
        altText: 'lorem ipsum 2',
        logo: `${config.contextPath}/assets/logo_bb.svg`
      };
    default:
      return {logo: null};
  }
})();

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article compare elements of an array of Objects and merge based on a condition [duplicate] Javascript – compare elements of an array of Objects and merge based on a condition [duplicate]
Next Article JavaScript: How to rewrite code to use dynamic values instead of hard-coded ones? Javascript – JavaScript: How to rewrite code to use dynamic values instead of hard-coded ones?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?