How to convert truthy/falsy to boolean (true/false)
JSsnippets

How to convert truthy/falsy to boolean (true/false)

const myVar = null; 
const mySecondVar = 1; 

console.log( Boolean(myVar) ) // false
console.log( !!myVar ) // false

console.log( Boolean(mySecondVar) ) // true

console.log( !!mySecondVar ) // true

If you know other methods please let me know, thanks.

I will post A JS tip per day! Please follow me on LinkedIn / Twitter / Facebook / Codesandbox / Codepen.



要查看或添加评论,请登录

Dan Ichim的更多文章

  • Draw a heart in CSS (#STOP COVID-19)

    Draw a heart in CSS (#STOP COVID-19)

    .heart { ? position: absolute; ? margin: auto; ? top: 0; ? right: 0; ? bottom: 0; ? left: 0; ? background-color:…

    2 条评论
  • The second argument of JSON.stringify lets you cherry-pick ?? keys to serialize.

    The second argument of JSON.stringify lets you cherry-pick ?? keys to serialize.

    const user = { id: 459, name: 'JS snippets', age:29, education:{ degree: 'Masters'…

  • URLSearchParams Web API

    URLSearchParams Web API

    The URLSearchParams interface defines utility methods to work with the query string of a URL. var paramsString =…

  • Nested Destructuring

    Nested Destructuring

    const user = { id: 325, name: 'JS Heroes', age: 28, passions: { tech: 'Public speaking', outside: 'Runing' } }…

    1 条评论
  • Copy Text to Clipboard

    Copy Text to Clipboard

    function copyToClipboard() { const copyText = document.getElementById("myInput"); copyText.

  • How to Change Screen Orientation with Javascript

    How to Change Screen Orientation with Javascript

    Demo is here, Source code is here Going to Full-Screen Mode It is necessary to go to full-screen mode before locking…

  • Get unique values in an array

    Get unique values in an array

    const numbers = [1,1,3,2,5,3,4,7,7,7,8]; //Ex1 const unieqNumbers = numbers.filter((v,i,a) => a.

  • Two ways to remove an item in a specific in an array

    Two ways to remove an item in a specific in an array

    //Mutating way const muatatedArray = ['a','b','c','d','e']; muatatedArray.splice(2,1) console.

  • Exploring the JavaScript Date Object

    Exploring the JavaScript Date Object

    There are hundreds of timezones in our world. In JavaScript, we only care about two—Local Time and Coordinated…

  • How to generate a random number in a given range

    How to generate a random number in a given range

    // Returns a random number(float) between min (inclusive) and max (exclusive)? const getRandomDoubleNumber = (min…

    1 条评论

社区洞察

其他会员也浏览了