• Search form is empty!

  • 005 TypeScript - var let const

    http://robertdunaway.github.io

    TypeScript code kata list

    All code kata lists

    Smiley face

    005 TypeScript - var let const

    Duration

    10 minutes

    Brief

    Working with var, let, and const and their scopes.

    For more information

    BING/GOOGLE: “TypeScript var let const”

    Instructions

    Get tutorial folder or the entire katas-typescript repo.
    Open the [before/*.sln] file and execute the kata.
    Feel free to execute this kata multiple times because repetition creates motor memory.

    Github

    Kata

    Create a few variables with “var”, “let”, and “const”.


    
        var myVar: number = 111;
        let myLet: number = 222;
        const myConst: number = 333;
    
    


    Demonstrate what happens when declaring a variable with “var” in the root and then again inside a block statement like foreach or if(true).
    Here is one possible example:


    
        if (true) {
            var myVar: number = 444;
        }
        console.log('myVar = ' + myVar);
    
    


    Demonstrate what happens when doing the same with a variable declared with “let”.


    
        if (true) {
            let myLet: number = 555;
        }
        console.log('myLet = ' + myLet);
    
    


    Smiley face


    Try changing the value of the constant you created earlier. You will receive error message, at design time, explaining why you can’t change the value.


    
        myConst = 123;
    
    


    Maybe you think you can just ignore this message and fall back on the Gulp task to transpile this errant TypeScript code.


    You’d be wrong…


    Smiley face


    Next

    Take a few minutes and imagine more examples.

    Smiley face

    0 comments:

    Post a Comment