
Cosine Similarity: Find How Similar Two Vectors Are
Editor | March 1, 2026 | 5 min read
Cosine similarity measures how similar two vectors are by comparing the angle between them, not their absolute size.
It is widely used in search, recommendation systems, NLP embeddings, and duplicate detection.
Why Cosine Similarity
If two vectors point in nearly the same direction, their cosine similarity is close to 1. If they are unrelated, it is near 0. If they point in opposite directions, it is near -1.
This makes cosine similarity useful when magnitude is less important than direction.
Formula
[ \text{cosineSimilarity}(A, B) = \frac{A \cdot B}{|A| \times |B|} ]
Where:
- (A \cdot B) is the dot product
- (|A|) is the magnitude (Euclidean norm) of vector A
- (|B|) is the magnitude of vector B
JavaScript Implementation
const dotProduct = (a, b) => a.reduce((sum, value, i) => sum + value * b[i], 0)
const magnitude = (v) => Math.sqrt(v.reduce((sum, value) => sum + value ** 2, 0))
export function cosineSimilarity(a, b) {
if (!Array.isArray(a) || !Array.isArray(b)) {
throw new TypeError("Inputs must be arrays")
}
if (a.length !== b.length) {
throw new Error("Vectors must have the same length")
}
const magA = magnitude(a)
const magB = magnitude(b)
if (magA === 0 || magB === 0) {
throw new Error("Cosine similarity is undefined for a zero vector")
}
return dotProduct(a, b) / (magA * magB)
}
Example
const vectorA = [1, 2, 3]
const vectorB = [2, 4, 6]
const score = cosineSimilarity(vectorA, vectorB)
console.log(score.toFixed(4)) // 1.0000
Another example:
const queryEmbedding = [0.12, -0.45, 0.33, 0.91]
const docEmbedding = [0.08, -0.5, 0.29, 0.88]
console.log(cosineSimilarity(queryEmbedding, docEmbedding))
Practical Notes
- Normalize or standardize input if your pipeline mixes scales.
- For text search, cosine similarity is usually applied to embedding vectors.
- Watch out for zero vectors after preprocessing; handle them explicitly.
Final Take
Use cosine similarity when you want direction-based similarity between vectors. It is simple, fast, and a strong default for many ML and search workflows.