Updated:

Ready-made commands

GoalJavaBedrock
Add 30 levels/xp add @s 30 levels/xp 30L @s
Add 500 points/xp add @s 500 points/xp 500 @s
Remove 10 levels/xp add @s -10 levels/xp -10L @s
Reset to level 0/xp set @s 0 levels/xp -10000L @s

Java writes add, target, amount, unit. Bedrock writes the amount first; an L right after the number means levels, no L means points.

Levels vs points

The green number is your level; the green bar is points. Filling the bar gives a level, and higher levels need more points. On Java, leaving out the unit means points, so /xp add @s 30 adds only 30 points. Add levels to raise levels.

Java commands

CommandMeaning
/xp add <target> <amount> levelsAdd levels (negative to remove)
/xp set <target> <amount> levelsSet an exact level
/xp query <target> levelsShow the current level

/xp set with points can only fill the current level's bar. To get a level plus a few points, set the level first, then add points. Java 1.12 and earlier used /xp 30L @p, which is why that form fails on current Java.

How many levels you need

UseLevels
Best enchanting table option30 (with 15 bookshelves; costs 3)
Anvil repairs and combiningSurvival refuses costs of 40 or more ("Too Expensive!")

Give XP as an item

/give @s minecraft:experience_bottle 64

Bottles o' enchanting drop XP orbs when thrown. As a map reward this feels more natural than /xp, and the orbs also repair Mending gear, which /xp does not. On Bedrock write /give @s experience_bottle 64.

Common problems

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.