Number 1/3
Numbers in JavaScript
JavaScript provides us with double-precision floating-point number to deal with the numbers in the code. You don’t need to specifically write int
or float
for declaring integer or floating values, unlike other programming languages.
12let totalPlayer=10; console.log(totalPlayer);
+
Operator with Numbers:
+
operator is used in two ways with the number data type.
Addition:
+
is used for addition when both the operands are numbers, for example.
1234let firstNumber = 10; let secondNumber = 20; let total = firstNumber + secondNumber; console.log(total);
Concatenation:
When one operand is a number while the other is a string, the+
operator concatenate them together, for example.
1234let company = 'Robotech '; let version = 10; let newLaunch = company + version; console.log(newLaunch);
JavaScript NaN and Infinity:
In JavaScript, the number datatype has some exceptions which are called “Special Numeric Values”. The exceptions are:
NaN:
NaN
stands for Not a Number.NaN
, in JavaScript, is a keyword that is used to check whether the value or variable is not a number.
We getNaN
as a result when we perform arithmetic operations other than+
between a numeric value and a string value, for example.
1234let numberOfStudent = 20; let greeting = 'Hello'; let total = numberOfStudent - greeting; console.log(total);
Infinity:
We getInfinity
or-Infinity
, in JavaScript, if the result of a calculation exceeds the largest possible number or the smallest possible number respectively, for example.
12console.log("2 / 0 :", 2 / 0) console.log("-2 / 0 :", -2 / 0)
In JavaScript, you can use the isFinite()
function to check whether a value or variable is Finite or not, for example.
1234let score = 123; let status = 'Test'; console.log(isFinite(score)); console.log(isFinite(status));
Note:
If isNaN()
returns true
, it means that the data type is not a number.
12let name = 'Bob'; console.log(isNaN(name));
Swipe to start coding
A variable named comment
with the assigned value of "hello123"
has been given to you. Your task is to check and print whether the comment
variable is a number or not as well as whether it is Finite or not.
Soluzione
Grazie per i tuoi commenti!
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione