===を使用する
===で文字列がtrueか判定します。大文字と小文字が区別されるため、toLowerCase()が必要な場合があります。
const boolString = "True"
const boolValue = (boolString.toLowerCase() === "true")
console.log(boolValue) // true
正規表現を使用する
以下のように正規表現を使用して文字列がtrueか判定します。
const boolString = "true"
const boolValue = (/true/).test(boolString)
console.log(boolValue) // true
大文字と小文字を区別しない場合には次のようにします。
const boolString = "True"
const boolValue = (/true/i).test(boolString)
console.log(boolValue) // true
!!を使用する
この方法は上記2つとは異なり、空文字か否かを判定します。
const stringValue1 = !!'true'
const stringValue2 = !!''
console.log(stringValue1) // true
console.log(stringValue2) // false
Boolean()を使用する
この方法も空文字か否かを判定します。
const stringValue1 = Boolean('true')
const stringValue2 = Boolean('')
console.log(stringValue1) // true
console.log(stringValue2) // false