Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to evaluate binomial coefficient of two integers n and k in JavaScript ?

  • Last Updated : 06 Apr, 2021

The following are the common definitions of Binomial Coefficients.

A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n. Binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects more formally, the number of k-element subsets (or k-combinations) of an n-element set.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Problem Statement: Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.

Approach: Below are the steps to be followed in order to create a function that returns the binomial coefficient of two integers.

  • Create a function that takes to arguments n and k.
  • Now check whether the n and k are numbers. If both or one of them are not number then return NaN.
  • Now check if k is less than 0 or k is greater than n. If one of the statements is true then return 0.
  • Now check if the k is equal to 1 or k is equal to n. If one of the statements is true then return 1.
  • Now check if is equal to 1 or k is equal to (n-1). If one of the statements is true then return n.
  • Now write the logic to get the binomial coefficient.



  function binomialCoefficient (n, k){
    // Checkuing if n and k are integer
    if(Number.isNaN(n) || Number.isNaN(k)){
      return NaN;
    if(k < 0 || k > n){
      return 0
    if(k === 0 || k === n){
      return 1
    if(k === 1 || k === n - 1){
      return n
    let res = n;
    for(let i = 2; i <= k; i++){
      res *= (n - i + 1) / i;
    return Math.round(res);
  console.log(binomialCoefficient(10, 2))


My Personal Notes arrow_drop_up
Recommended Articles
Page :