Boolean()を使ってコードの見通しをよくする例

date
Feb 27, 2023
repo_url
slug
code-boolean
status
Published
summary
type
Post
thumbnail_url
tags
javascript
outer_link

冗長な書き方と改善

const checkString = ""
 
if(checkString) {
    return true
  } else {
    return false
  }

//↓

const checkString = ""
return Boolean(checkString)
const checkString = ""
const checkHoge = checkString ? true : false

//↓

const checkString = ""
const checkHoge = Boolean(checkString)
//二重否定の書き方もある↓
const checkHoge = !!checkString
 

© Hayato Kamiyama 2023 - 2024 - Build with Next.js & Notion