Skip to content
tezvyn:
Menu

Conditional logic with Platform.Version

Source: interviewMediumHow cards are made

Summary

OS-version-aware branching.

Key points

Platform.Version is a number on Android (API level) and a string on iOS; gate features by version to use new APIs only where supported.

Watch out for

comparing the iOS string version as a number.

What's really being asked

Whether you can gate behavior on the operating system version, knowing that Platform.Version has different types per platform and that version is a proxy, not a direct measure, of device features.

The full answer

Platform.Version returns the Android API level as a number, so a numeric comparison works directly, while on iOS it returns the version as a string like '17.4', which you typically parse before comparing. You use it to call APIs introduced in a specific OS version only where they exist and to fall back otherwise. A common Android case is requesting a permission added at a certain API level, such as POST_NOTIFICATIONS on Android 13 (API 33), only when Platform.Version is at least 33. For layout, prefer react-native-safe-area-context to detect actual insets rather than inferring a notch from the OS version, since version does not guarantee a specific device shape.

The mistakes people make

Comparing the iOS string version with a numeric operator without parsing, assuming the same type on both platforms, or inferring hardware features like a notch purely from OS version. Hardcoding magic numbers without comments about which API level they represent reduces clarity.

What usually comes next

Why is detecting safe-area insets better than inferring a notch from version? How do you write a clean comparison given the type difference? When does a feature check beat a version check?

A concrete example

if (Platform.OS === 'android' && Platform.Version >= 33) { await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS); } gates the runtime notification permission to Android 13 and newer, where it exists, while older devices skip it. On iOS you would parseInt(Platform.Version, 10) before comparing, because the value is a string, not a number.

Interview question

What mistake can occur when comparing Platform.Version directly with a numeric operator on iOS?

  • a.iOS returns a Date object that cannot be compared at all
  • b.iOS returns the version as a string, so a numeric comparison may behave unexpectedly without parsingCorrect
  • c.Platform.Version is undefined on iOS, throwing a runtime error
  • d.iOS reports the Android API level by mistake, giving a wrong number
Why?

On iOS Platform.Version is a string like '17.4', so you should parse it before numeric comparison, whereas Android returns a numeric API level. It is neither a Date, undefined, nor an Android API level, so those options are wrong.

Just read this? Test yourself on what you have been reading.

Read the original → reactnative.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles