$30 off During Our Annual Pro Sale. View Details »

if文のはなし

tky
March 22, 2019

 if文のはなし

tky

March 22, 2019
Tweet

Other Decks in Programming

Transcript

  1. if文のはなし
    @ticktakclock

    View Slide

  2. 自己紹介
    ● 竹中貴哉
    @ticktakclock

    React, Cordova

    javascript
    始めて
    6
    ヶ月経ちました
    ● 本業は
    Android

    View Slide

  3. if ...

    View Slide

  4. const str = 'we are javascripters';
    console.log(`length: ${str.length}`);
    if (str.length > 0) { // true
    console.log('ok');
    }
    // > length: 20
    // > ok

    View Slide

  5. const str = 'we are javascripters';
    if (str.startsWith('we')) { // true
    console.log('ok');
    }
    // > ok

    View Slide

  6. if文はtrue/falseで判定するんだ!

    View Slide

  7. const str = 'we are javascripters';
    console.log(str);
    if (str) {
    console.log('ok');
    }
    // > we are javascripters
    // > ok

    View Slide

  8. const str = 'we are javascripters';
    console.log(str);
    if (str !== null) {
    console.log('ok');
    }
    // > we are javascripters
    // > ok

    View Slide

  9. const str = 'we are javascripters';
    console.log(str);
    if (str) {
    console.log('ok');
    }
    // > we are javascripters
    // > ok

    View Slide

  10. ドキュメント
    https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/if...else#Syntax

    View Slide

  11. truthy? falsy?

    View Slide

  12. truthy
    https://developer.mozilla.org/ja/docs/Glossary/truthy

    View Slide

  13. const str = 'we are javascripters';
    console.log(str);
    if (str !== null) {
    console.log('ok');
    }
    // > we are javascripters
    // > ok

    View Slide

  14. const str = 'we are javascripters';
    console.log(str);
    if (str) {
    console.log('ok');
    }
    // > we are javascripters
    // > ok

    View Slide

  15. やってみた
    console.log(0 ? 'truthy' : 'falsy'); // falsy
    console.log(1 ? 'truthy' : 'falsy'); // truthy
    console.log(-1 ? 'truthy' : 'falsy'); // truthy
    console.log('' ? 'truthy' : 'falsy'); // falsy
    console.log('test' ? 'truthy' : 'falsy'); // truthy
    console.log(undefined ? 'truthy' : 'falsy'); // falsy
    console.log(null ? 'truthy' : 'falsy'); // falsy
    console.log('undefined' ? 'truthy' : 'falsy'); // truthy
    console.log(new Date() ? 'truthy' : 'falsy'); // truthy

    View Slide

  16. まとめ
    if文の条件式は何でも入る

    View Slide

  17. string
    https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%
    20June%201997.pdf

    View Slide

  18. おしまい

    View Slide

  19. string
    https://developer.mozilla.org/ja/docs/Web/JavaScript/Data_structures#Strings

    View Slide

  20. var str = '';
    console.log(str[0]); // undefined
    console.log(str.length); // 0
    console.log(str.charCodeAt(0)); // NaN
    str = '\0';
    console.log(str[0]); // ’ ’
    console.log(str.length); // 1
    console.log(str.charCodeAt(0)); // 0

    View Slide