Fundamental JavaScript Programming Concepts

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the fundamental concepts of JavaScript programming. You will learn how to write basic JavaScript code and gain an understanding of variables, data types, functions, and control structures. By the end of this lab, you will have a solid foundation in JavaScript programming and be able to apply your knowledge to solve real-world problems.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28222{{"`Fundamental JavaScript Programming Concepts`"}} javascript/data_types -.-> lab-28222{{"`Fundamental JavaScript Programming Concepts`"}} javascript/arith_ops -.-> lab-28222{{"`Fundamental JavaScript Programming Concepts`"}} javascript/comp_ops -.-> lab-28222{{"`Fundamental JavaScript Programming Concepts`"}} javascript/higher_funcs -.-> lab-28222{{"`Fundamental JavaScript Programming Concepts`"}} end

How to Count Occurrences in JavaScript

To count the number of times a specific value occurs in a JavaScript array, you can use the Array.prototype.reduce() method.

Here is how you can do it:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Copy and paste the following code:
const countOccurrences = (arr, val) =>
  arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
  1. In the code above, the countOccurrences function takes two arguments: the array to search and the value to count.
  2. The reduce() method is used to loop through each element in the array and increment a counter each time the specific value is encountered.
  3. To test the function, call it with an array and a value, like this:
countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3

This will return the number of times 1 occurs in the array [1, 1, 2, 1, 2, 3], which is 3.

Summary

Congratulations! You have completed the Count Occurrences lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like