Count Character Occurrences in a String using JavaScript

varun
Updated on 17-Jun-2020 06:39:27

851 Views

To count the number of occurrences of a character in a string, try to run the following code −ExampleLive Demo                    function displayCount() {             setTimeout(function() {                var str = document.getElementById('str1').value;                var letter = document.getElementById('letter1').value;                letter = letter[letter.length-1];                var lCount;                for (var i = lCount = 0; i < str.length; lCount += (str[i++] == letter));                document.querySelector('p').innerText = lCount;                return lCount;             }, 50);          }             Enter String:       Enter Letter:       Click for Result       result=    

Best Way to Initialize a JavaScript Number

Prabhas
Updated on 17-Jun-2020 06:38:06

789 Views

The best way to initialize a number in JavaScript is to initialize variables while declaring them. Through this, you can easily avoid undefined values.ExampleYou can try to run the following code to initialize a number −Live Demo                    var deptNum = 20, id = 005;          document.write("Department number: "+deptNum);          

Convert Number to String in JavaScript

vanithasree
Updated on 17-Jun-2020 06:37:14

267 Views

The best way to convert a number to a string is using the toString() method, with different bases. The method has an optional parameter “radix”, which is used as a base (2, 8, 16) for a numeric value.ExampleYou can try to run the following code to convert a number to a string −Live Demo                    var num = 10;          var val1 = num.toString();          var val2 = num.toString(2);          var val3 = num.toString(8);          var val4 = num.toString(16);          document.write(val1 + "" + val2 + "" + val3 + "" + val4);           Output10 1010 12 a

Get Decimal Portion of a Number with JavaScript

mkotla
Updated on 17-Jun-2020 06:35:57

3K+ Views

With JavaScript, use the % operator to get the decimal portion of a number.ExampleYou can try to run the following to get decimal portion −Live Demo                    var num1 = 5.3;          var num2 = 4.2;          var num3 = 8.6;          document.write(num1 % 1);          document.write(""+num2 % 1);          document.write(""+num3 % 1);           Output0.2999999999999998 0.20000000000000018 0.5999999999999996

Format Hexadecimal Number in JavaScript

Giri Raju
Updated on 17-Jun-2020 06:35:28

2K+ Views

Firstly convert your number to hexadecimal with leading zeros and then format it by adding dashes.ExampleYou can try to run the following code to format hexadecimal number −Live Demo                    var num = 32122;          var myHex = ("000000000000000" + num.toString(16)).substr(-16);          var resHex = myHex.substr(0, 8)+'-'+myHex.substr(8,4)+'-'+myHex.substr(12,4);          document.write(resHex);           Output00000000-0000-7d7a

Get the Length of a Number in JavaScript

Vrundesha Joshi
Updated on 17-Jun-2020 06:34:53

19K+ Views

Use the toString() method to covert the Number to string, then the length() method gives you length.ExampleYou can try to run the following code to learn how to get the length of a Number −Live Demo                    var num1 = 344;          var num2 = 149856;          document.write("Length of number "+num1+" = "+ num1.toString().length);          document.write("Length of number "+num2+" = "+ num2.toString().length);           OutputLength of number 344 = 3 Length of number 149856 = 6

Check if a Number is NaN in JavaScript

Krantik Chavan
Updated on 17-Jun-2020 06:30:40

320 Views

NaN is a JavaScript property, which is Not-a-Number value. This shows it is not a legal number. Here’s the syntax −SyntaxNumber.NaNTo find out whether a number is NaN, use the Number.isNaN() or isNan() method. Here’s an example to check −ExampleLive Demo           Check                      function display() {             var a = "";             a = a + isNaN(434) + ": 434";             a = a + isNaN(-23.1) + ": -23.1";             a = a + isNaN('Hello') + ": 'Hello'";             a = a + isNaN(NaN) + ": NaN";             a = a + isNaN('') + ": ''";             a = a + isNaN(0) + ": 0";             a = a + isNaN(false) + ": false";             document.getElementById("test").innerHTML = a;          }          

Calculate Difference Between Two Dates in JavaScript

Jennifer Nicholas
Updated on 17-Jun-2020 06:27:09

2K+ Views

To get dates in JavaScript, use the getTime() method. Forgetting the difference between two dates, calculate the difference between date and time.ExampleYou can try to run the following code to learn how to calculate a difference between two dates −Live Demo                    var dateFirst = new Date("11/25/2017");          var dateSecond = new Date("11/28/2017");          // time difference          var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());          // days difference          var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));          // difference          alert(diffDays);          

Get Number Value of a String in JavaScript

Nancy Den
Updated on 17-Jun-2020 06:24:14

564 Views

To get an integer in the string “abcde 30”, use the following regex in JavaScript, else it will return NaN. With that, use parseInt() to get the numbers with regex under String match() method −ExampleLive Demo                    var myStr = "abcdef 30";          var num = parseInt(myStr.match(/\d+/))          alert(num);          

Get String Representation of a Number in JavaScript

Daniol Thomas
Updated on 17-Jun-2020 06:23:34

516 Views

Use the toString() method to get the string representation of a number. You can try to run the following code to print a string representation of numbers like 20, -40, 15.5 −ExampleLive Demo                    var num1 = 25;          document.write(num1.toString()+"");          document.write((30.2).toString()+"");          var num2 = 3;          document.write(num2.toString(2)+"");          document.write((-0xff).toString(2));           Output25 30.2 11 -11111111Above, you can see we added a parameter to the toString() method. This is an optional parameter, which allows you to add an integer between 2 and 36, which is the base for representing numeric values.

Advertisements