CodeBlanks Logo Icon CodeBlanks

Semver

Learn semver and ^/~ version ranges.

Semver

semver (semantic versioning) defines the meaning of version numbers. A version has the form major.minor.patch, for example 4.18.0.

A changing major means a breaking change, a higher minor means a new feature, and a higher patch means a bug fix. When giving a version range in package.json, two symbols are common: ^ (caret) allows updates within the same major; ~ (tilde) allows only patch-level updates. For example ^4.18.0 accepts any version within 4.x; ~4.18.0 accepts only 4.18.x. This way updates are received safely.

Pinning an exact version

Sometimes you want to pin the exact version of a package. Without ^ or ~, writing only 4.18.0 makes npm load just that version; no updates.

This is important for reproducible builds; every install uses the same version. But it also blocks security updates. You can also use wildcards like 1.x or 1.2.x: 1.x accepts any version from 1 up to (but not including) 2. Which range you choose depends on the trade-off between security and stability. Usually ^ is recommended, but in critical systems pinning is safer.

Practice Print the major version

Define a string named version with the value "4.18.0". Take the major part with .split(".")[0] and print it with console.log.

Starter code

index.js
// Define the version string
// Split it on dots
// Print the first part (major)
// write here

Sample solution

index.js
const version = "4.18.0";
const major = version.split(".")[0];
console.log(major);

Expected output

4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the ^4.18.0 version range allow?

index.js
^4.18.0
  • 4.x (4.18.0 and later 4.x) Correct answer
  • 4.x and 5.x
  • Only 4.18.0
  • Any version

Explanation: ^ allows updates within the same major.

2 Complete the caret range

Question
"____4.18.0"
Answer
"^4.18.0"

Explanation: ^4.18.0 allows updates within the same major.

3 Match each range to its behavior

Term Meaning
^1.2.3 Allows minor and patch
~1.2.3 Allows only patch
1.2.3 Pins the exact version
1.x Minor family

All lessons in Intermediate