Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Number 1/3 | Data Types and Variables
Introduction to JavaScript (staging)
course content

Cursusinhoud

Introduction to JavaScript (staging)

Introduction to JavaScript (staging)

1. Introduction
2. JavaScript syntax
3. Data Types and Variables

book
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.

12
let totalPlayer=10; console.log(totalPlayer);
copy

+ 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.
1234
let firstNumber = 10; let secondNumber = 20; let total = firstNumber + secondNumber; console.log(total);
copy
  • Concatenation:
    When one operand is a number while the other is a string, the + operator concatenate them together, for example.
1234
let company = 'Robotech '; let version = 10; let newLaunch = company + version; console.log(newLaunch);
copy

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 get NaN as a result when we perform arithmetic operations other than + between a numeric value and a string value, for example.
1234
let numberOfStudent = 20; let greeting = 'Hello'; let total = numberOfStudent - greeting; console.log(total);
copy
  • Infinity:
    We get Infinity or -Infinity, in JavaScript, if the result of a calculation exceeds the largest possible number or the smallest possible number respectively, for example.
12
console.log("2 / 0 :", 2 / 0) console.log("-2 / 0 :", -2 / 0)
copy

In JavaScript, you can use the isFinite() function to check whether a value or variable is Finite or not, for example.

1234
let score = 123; let status = 'Test'; console.log(isFinite(score)); console.log(isFinite(status));
copy

Note:
If isNaN() returns true, it means that the data type is not a number.

12
let name = 'Bob'; console.log(isNaN(name));
copy
Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 12
toggle bottom row

book
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.

12
let totalPlayer=10; console.log(totalPlayer);
copy

+ 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.
1234
let firstNumber = 10; let secondNumber = 20; let total = firstNumber + secondNumber; console.log(total);
copy
  • Concatenation:
    When one operand is a number while the other is a string, the + operator concatenate them together, for example.
1234
let company = 'Robotech '; let version = 10; let newLaunch = company + version; console.log(newLaunch);
copy

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 get NaN as a result when we perform arithmetic operations other than + between a numeric value and a string value, for example.
1234
let numberOfStudent = 20; let greeting = 'Hello'; let total = numberOfStudent - greeting; console.log(total);
copy
  • Infinity:
    We get Infinity or -Infinity, in JavaScript, if the result of a calculation exceeds the largest possible number or the smallest possible number respectively, for example.
12
console.log("2 / 0 :", 2 / 0) console.log("-2 / 0 :", -2 / 0)
copy

In JavaScript, you can use the isFinite() function to check whether a value or variable is Finite or not, for example.

1234
let score = 123; let status = 'Test'; console.log(isFinite(score)); console.log(isFinite(status));
copy

Note:
If isNaN() returns true, it means that the data type is not a number.

12
let name = 'Bob'; console.log(isNaN(name));
copy
Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 12
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt