1

I am going through a book about HTML5 and there are two lines of code I cannot quite understand

var mp3Support,oggSupport;
var audio = document.createElement('audio');
if(audio.canPlayType) {
        mp3Support = "" != audio.canPlayType('audio/mp3');
}

So, first you create an audio element and check if canPlayType method can be used? Then, the code inside the if statement is some kind of ternary operation?

The audio.canPlayType('audio/mp3') outputs 'probably' and mp3Support is set to '' but after that line mp3Support outputs true. Any tips would be much appreciated.

0

2 Answers 2

2

So, first you create an audio element

Yes

and check if canPlayType method can be used?

The check is to see if canPlayType is a true value, but that amounts to the same thing in practical terms.

Then, the code inside the if statement is some kind of ternary operation?

No.

audio.canPlayType('audio/mp3') can return a number of values, one of which is an empty string.

"" != audio.canPlayType('audio/mp3'); tests to see if it not an empty string (and evaluates as true or false)

mp3Support = then is just assigned that true or false

It could be more clearly written as:

mp3Support = ("" != audio.canPlayType('audio/mp3'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the really thorough explanation!
1
 mp3Support = "" != audio.canPlayType('audio/mp3');

This combines a variable initialization with boolean expression.

in other way:

if(audio.canPlayType('audio/mp3')!="")
{
  mp3Support=true;
}
else
{
  mp3Support=false;
}

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.