[JS] 05. Conditional_Code

2017. 8. 28. 13:35·JS/JavaScript
728x90
반응형

05_Conditional_Code.html

<script>
//Flow control
var foo = true;
var bar = false;

if ( bar ) {
    // This code will never run.
    console.log( "hello!" );
}

if ( bar ) { 
    // This code won't run. 
} else { 
    if ( foo ) {
        // This code will run.
    } else {
        // This code would run if foo and bar were both false.
    } 
}



// [Truthy and Falsy Things]
// Values that evaluate to false:
false;
""; // An empty string.
NaN; // JavaScript's "not-a-number" variable.
null;
undefined; // Be careful -- undefined can be redefined!
0; // The number zero.

//Everything else evaluates to true, some examples:
"0";
"any string";
[]; // An empty array.
{}; // An empty object.
1; // Any non-zero number.



// [Conditional Variable Assignment with the Ternary Operator]
//Set foo to 1 if bar is true; otherwise, set foo to 0:
var foo = bar ? 1 : 0;



// [Switch Statements]
switch ( bar ) { 
    case "bar":
        alert( "the value was bar -- yay!" );
        break;

    case "baz":
        alert( "boo baz :(" );
        break;

    default:
        alert( "everything else is just ok" );

}

//Switch statements have somewhat fallen out of favor in JavaScript, 
//because often the same behavior can be accomplished by creating an object 
//that has more potential for reuse or testing. For example:
var stuffToDo = {   
     "bar": function() {
         alert( "the value was bar -- yay!" );
     },
  
     "baz": function() {
         alert( "boo baz :(" );
     },
  
     "default": function() {
         alert( "everything else is just ok" );
     }
  
 };
  
 // Check if the property exists in the object.
 if ( stuffToDo[ "foo" ] ) {
     // This code won't run.
     stuffToDo[ "foo" ]();  
 } else {
     // This code will run.
     stuffToDo[ "default" ]();  
 }
</script>
 

728x90
반응형

'JS > JavaScript' 카테고리의 다른 글

[JS] 07. Array  (0) 2017.08.28
[JS] 06. Loop  (0) 2017.08.28
[JS] 04. Operator  (0) 2017.08.28
[JS] 03. Type  (0) 2017.08.28
[JS] 02. SyntaxBasic  (0) 2017.08.28
'JS/JavaScript' 카테고리의 다른 글
  • [JS] 07. Array
  • [JS] 06. Loop
  • [JS] 04. Operator
  • [JS] 03. Type
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (407) N
      • Flutter (2)
      • 일상생활 (8)
        • 리뷰 (1)
        • 생활정보 (4)
        • 맛집 (0)
        • 여행 (0)
        • 모든정보 (3)
      • JAVA (126)
        • 개념 (6)
        • 예제 (115)
        • Exception (2)
      • C (1)
        • C (1)
        • C++ (0)
        • C# (0)
      • JS (29)
        • JavaScript (18)
        • JQuery (5)
        • AJax (0)
        • NODE.JS (6)
        • Angular.JS 2.0 (0)
      • WEB (87)
        • HTML (6)
        • CSS (61)
        • JSP (20)
        • JSTL (0)
      • FrameWork (8)
        • Spring (8)
        • BootStrap (0)
        • MyBATIS (0)
        • JUnit (0)
      • 외부 라이브러리 (5)
      • 공유 소스 관리 (5)
        • Git (5)
        • SVN (0)
      • 빅데이터 프로그래밍 (37)
        • Python (37)
        • R Programming (0)
      • DB (7)
        • ORACLE (0)
        • MySql (6)
      • Development Tools (7)
        • StarUML (0)
        • eXERD (0)
        • Eclipse (4)
      • SKILL (6)
        • Migration (0)
        • Security (6)
      • MicroSoft (0)
        • Excel (0)
        • Word (0)
      • Android (0)
      • Server (21)
        • Ubuntu (5)
        • Linux (15)
      • IOS (0)
      • XML (0)
      • 미디어 (0)
      • 공지사항 (3)
      • NETWORK (1)
      • 게임 (4)
        • 피파 (1)
        • 리니지M (0)
        • 배틀그라운드 (1)
        • 듀랑고 (2)
      • 세상 이슈 (6)
      • 일렉트론 (0)
      • 대회 소식 (4)
      • 업무 (2)
      • Express, Vue (6)
      • docker (11)
      • svelte (3)
      • 블록체인 (1)
      • IT (10) N
      • Rust (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    css list
    Java Array
    gitlab 설치
    nginx ssl 설정
    에디터 팁
    VS Code 팁
    vue 설치
    css float
    Node
    css block
    ubuntu
    React Compiler
    vue cli
    자바 객체 지향
    SSL 인증서
    lang rust
    mysql db
    servlet class
    vscode
    프런트엔드
    API 설계
    자바 클래스
    AI 코딩 에이전트
    리눅스 설치
    jsp parameter
    자바 for문
    docker
    nginx ssl 적용
    클론코딩
    java casting
    rust linux
    ssl 인증서 발급
    css perspective
    spring java
    svelte
    브라우저 자동화
    오류
    자바 생성자
    jsp include
    Rust lang
    러스트
    자바 배열
    css tb
    티스토리 자동화
    Extension Bisect
    proxy pass
    nginx
    css table
    자바 exception
    css transition
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
밍글링글링
[JS] 05. Conditional_Code
상단으로

티스토리툴바