最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can i use multiple line in ternary operator - Stack Overflow

programmeradmin1浏览0评论

I want to use the if else statement in the ternary operator

     if (open) {
         setOpen(false)
     } else {
         setOpen(true)
         navigator.clipboard.writeText(link)
     }

There is no problem in "if" I cant figuring out how to convert else to ternary. Like something the code below:

open ? setOpen(false) : setOpen(true) ; navigator.clipboard.writeText(link)

Something like this or is there another method to do the job?

I want to use the if else statement in the ternary operator

     if (open) {
         setOpen(false)
     } else {
         setOpen(true)
         navigator.clipboard.writeText(link)
     }

There is no problem in "if" I cant figuring out how to convert else to ternary. Like something the code below:

open ? setOpen(false) : setOpen(true) ; navigator.clipboard.writeText(link)

Something like this or is there another method to do the job?

Share Improve this question asked Jun 6, 2022 at 17:26 Shayan AffandiShayan Affandi 631 gold badge1 silver badge4 bronze badges 3
  • 1 Ternary is for one-liners. Dont overthink it. If it is not a one-liner just write good old if-else. – Ensar Commented Jun 6, 2022 at 17:31
  • 1 Don't cram multiple lines onto one. What possible reason would you have for doing this? Lines cost nothing. You're just making taking clear, obvious code and making it brittle and prone to bugs. – user229044 Commented Jun 6, 2022 at 17:37
  • Similar discussion (somewhat language-agnostic) – cellepo Commented Jan 4, 2024 at 1:13
Add a comment  | 

4 Answers 4

Reset to default 8

Yes. it is possible (although not a best practice and not recommended)

they way to to it is by:

  1. Put everything inside parenthesis
  2. Seperate each statement with comma (",")

e.g:

condition ? statement1 : ( statement2, statement3, statement4 )

Try this snippet:

let a = 1;
let b = 1;
a == b ?
(console.log("they"),console.log("are"), console.log("equal")) :
(console.log("they're"), console.log("not equal"));

Don't.

You're trying to use the ternary conditional operator for the wrong reason. It is not a drop-in replacement for any if block.

The ternary conditional operator is an expression. It resolves to a value, which can be used elsewhere. For example:

let x = someCondition ? 1 : 0;

The expression resolves to a value, either 1 or 0, and that value is used in an assignment statement.

The code you're showing is not an expression. What you have is a series of statements, conditionally executed based on some value. An if block is a structure for conditionally executing statements.

The code you have now is correct.

this might do the trick

true ? (() => { 
      console.log(1);
      console.log(2);
      console.log(3);
    })() : false

Yes, it's possible to write multiple statements in ternary if else cases:

The format is:

condition ? codeLine1 : ( codeLine2 , codeLine3 )

Which makes your statement as:

open ? setOpen(false) : (setOpen(true), navigator.clipboard.writeText(link));

Combine multiple statements in parenthesis separated by commas in between each line.

That being said it's recommended to use old fashioned way of if-else statement if multiple statements are involved.

Please select answer if it helps and let me know if any questions.

发布评论

评论列表(0)

  1. 暂无评论