Simple arithmetic operations with numbers can be performed with specific operators: Subtraction (-), addition (+), multiplication (*), division (/) and modulo (%) the rest of the division. See the lesson from: coursesweb.net/javascript/operators

- Example, gets the sum of three numbers, then the rest of division with 3.
const n1 =2;
const n2 =9;

//sum
let sum_n = n1 +n2 +15; // 26

//rest of division with 3
let rest3 = sum_n %3; // 2

document.write('Sum (n1+n2+15) = '+sum_n+'<br>Rest of this sum divided by 3 is: '+rest3);

Numeric strings

Numeric strings (numbers added between quotes) cannot be used to perform correctly mathematical operations.
JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated

- Example, trying to add a numeric string to a number, it results a string with the numbers joined:
let n1 ='21';
let n2 =9;

//sum
let sum_n = n1 +n2; //219

document.write("Sum ('21'+ 9) ="+sum_n);

Before performing mathematical operations with numeric strings, they must be converted to Number.
The simplest way to convert a numeric string to Number is by: multiplying the string by 1, or with the Number() function.

- Example:
let n1 ='21';
let n2 =9;

n1 = n1*1; //converts to number
let sum_n = n1 +n2; //30

document.write('Sum (21+9) = '+sum_n);
The same example, with the Number() function:
let n1 ='21';
let n2 =9;

n1 = Number(n1); //converts to number
let sum_n = n1 +n2; //30

document.write('Sum of (21+9) = '+sum_n);

Functions for the Number object

Numbers without decimal are considered Integer, and the decimal numbers are considered Float.
The negative numbers are added with the minus sign (-).
Number object has some methods for numbers.

Number.isInteger(nr) - returns True if 'nr' is an Integer, otherwise, False.
var n1 = 90;
var n2 = -25;
var n3 = 90.23;
var n4 ='23';

console.log(Number.isInteger(n1)); // true 
console.log(Number.isInteger(n2)); // true
console.log(Number.isInteger(n3)); // false
console.log(Number.isInteger(n4)); // false
Number(sn) - converts a numeric string 'sn' to number. returns NaN (Not a Number) if the string is not numeric.
var n1 ='57.98';
var n2 ='23 str';

console.log(Number(n1)); // 57.98
console.log(Number(n2)); // NaN
parseFloat(sn) - parses an argument and returns a floating point number.
var nr ='13.56';
nr = parseFloat(nr); //number 13.56
var sum_n = nr +8.2;

document.write('- Suma: nr +8 = '+sum_n); //21.759999999999998
parseInt(sn) - parses a string argument and returns a string with an integer number.
var n1 ='25.89';
var n2 ='-34.8 23';

console.log(parseInt(n1)); // 25
console.log(parseInt(n2)); // -34
nr.toFixed(d) - returns a string representing the given number using fixed-point notation, with the specified number of decimals 'd'.
- This method is applied to numbers, not to 'numeric strings'.
var n1 = 92;
var n2 = -25.5689;
var n3 = 90.2378;

console.log(n1.toFixed(2)); // 92.00 
console.log(n2.toFixed(1)); // -25.6
console.log(n3.toFixed(2)); // 90.24
nr.toPrecision(n) - returns a string with the number from 'nr', having:
- for 'nr'>1, the specified length of digits 'n'.:
- for 'nr'<1, the specified length of decimals 'n'.
var n1 = 24;
var n2 = -3.23;
var n3 = 0.46;

console.log(n1.toPrecision(3)); // 24.0
console.log(n2.toFixed(1)); // -3.2
console.log(n3.toFixed(3)); // 0.460
nr.toString() - returns a string with the number from 'nr'.
var n1 = 12.5;
var sum_n = n1.toString() +3;
var ex2 = (12 + 9).toString();

console.log(sum_n); // 12.53
console.log(ex2); // 21

The Number() function with the Date object

The Number() applied with the Date object returns the number of milliseconds since 1.1.1970 till the time from the Date object.
var dt1 = new Date('2018-07-15');
var dt2 = new Date('2018-07-15 11:13:00');

console.log(Number(dt1)); // 1531612800000
console.log(Number(dt2)); // 1531642380000

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
Numbers in JavaScript

Last accessed pages

  1. Node.js Move and Copy Directory (1928)
  2. Script Users Register, Login, Online (16475)
  3. innerHTML and outerHTML to Get and Replace HTML content (18725)
  4. Simple Admin Login PHP Script (1209)
  5. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (12107)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (558)
  2. Read Excel file data in PHP - PhpExcelReader (467)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (343)
  4. PHP getElementById and getElementsByTagName (266)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (254)