0

Code:

function fun(example:string|number|string[])
{
    if(typeof example == "string")
    {
        console.log("Example is String " + example);
    }
    else if(typeof example == "number")
    {
        console.log("Example is Number " + example);
    }
    else{
        console.log("Example is String Array " + example);
    }
}
var example:number; //Here i have assigned the var as a number
fun(example); 

Output:

Example is String Array undefined

I have given the var type as a "number", but why the output screen shows "String array"?

1
  • you doesnt have assigned anything, you have just declared it. undefined gets assigned automatically if you dont assign it on your own Commented Jun 3, 2020 at 5:34

4 Answers 4

1

You haven't given a value to the variable example, hence by default it's undefined, that's why your code runs the last else statement.

Try with:

var example = 3
fun(example)
// Example is Number 3

Remember that type annotations are used at compile time. This means that when your code runs, it doesn't know anything about the types you declared.

Sign up to request clarification or add additional context in comments.

3 Comments

I think in this case, the "undefined" should be considered as String, but the output is showing the String Array, can you explain this why?
@Dineshkapri It's printing "Example is String Array" because that's exactly what you have written in the quotes. Try printing "Example is" + typeof example instead.
@Dineshkapri I think you're messing with values. undefined is the value (or the non-value), it can't be considered a string because it just isn't. If it was "undefined" (with quotation marks), then yes, it should be string.
0

Even though you just declared a number variable but you haven't assigned that variable a number, as we know very well we don't have any default value of the number in typescript It would be undefined and the property that you have defined for the variable is not the property of that variable It will be the property of the value that will be assigned to that veriable.

Comments

0

In TypeScript, indeed we can assign the "type" to a variable at the start; however, as we all know that ultimately a TypeScript file gets converted into the JavaScript (so that our browser can understand and work over that) and the default value assigned to a variable which hasn't assigned any value is undefined.

var example:number; line assigns the variable type but doesn't assigns the value to variable example and hence it gets the undefined at execution time and enters the last else condition and prints: Example is String Array undefined.

Comments

0

pass a number in the function instead of an example variable(that doesn't hold a value), y it shows string array as type? bcz when its undefined, the typeof undefined doesn't match with string or number, hence it goes to the else block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.