Declaringjsdatatypes.md - brainchildservices/curriculum GitHub Wiki

Slide 1

JavaScript String

Strings are used to store data that involves characters, like names or addresses. You can perform operations like string concatenation, in JavaScript. Strings are written with quotes. We can use single or double quotes. String is used to store text. In JavaScript, strings are surrounded by quotes:

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Backticks: Hello

For example,

 //strings example

 const name = 'ram';
 const name1 = "hari";
 const result = `The names are ${name} and ${name1}`;

Single quotes and double quotes are practically the same and you can use either of them.

Slide 2

Wrong Syntax Example

var str = Hello;
var str2 = 'Single quotes are ok too';
var phrase = str + " " + str2; //concatenation of strings 
document.write(phrase)

Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression} as shown above.

You will learn about the use of backticks in the JavaScript String tutorial.

Slide 3

JavaScript Number

Number type stands for both integers and floating points. Many mathematical operations are carried out using these numbers. Number, representsinteger and floating numbers (decimals and exponentials). For example,

 const number1 = 3;
 const number2 = 3.433;
 const number3 = 3e5 // 3 * 10^5

A number type can also be +Infinity, -Infinity, and NaN (not a number). For example,

 const number1 = 3/0;
 console.log(number1); // Infinity

Slide 4

 const number2 = -3/0;
 console.log(number2); // -Infinity

 // strings can't be divided by numbers
 const number3 = "abc"/3; 
 console.log(number3);  // NaN

Wrong Syntax Example

 const x1 = "34.00";// In quotes, represents as strings, not a number
 const x2 = "34";//In quotes, represents as strings, not a number

Slide 5

JavaScript BigInt

In JavaScript, Number type can only represent numbers less than (253 - 1) and more than -(253 - 1). However, if you need to use a larger number than that, you can use the BigInt data type.

A BigInt number is created by appending n to the end of an integer. For example,

 // BigInt value
 const value1 = 900719925124740998n;

 // Adding two big integers
 const result1 = value1 + 1n;
 console.log(result1); // "900719925124740999n"

 const value2 = 900719925124740998n;

 // Error! BitInt and number cannot be added
 const result2 = value2 + 1; 
 console.log(result2); // BigInt value
 const value1 = 900719925124740998n;

Slide 6

 // Adding two big integers
 const result1 = value1 + 1n;
 console.log(result1); // "900719925124740999n"

 const value2 = 900719925124740998n;

Wrong Syntax Example

 // Error! BitInt and number cannot be added
 const result2 = value2 + 1; 
 console.log(result2); 

Output

 900719925124740999n
 Uncaught TypeError: Cannot mix BigInt and other types

Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers including Safari

Slide 7

JavaScript Boolean

This data type represents logical entities. Boolean represents one of two values: true or false. This type is usually used to check if something is correct or incorrect.

For example,

const dataChecked = true;
const valueCounted  = false;

Example

var x = 5;
var y = 5;
var z = 6;
(x == y)       // Returns true
(x == z)       // Returns false

Wrong Syntax Example

 var x = 5;
 var y = 5;
 var z = 6;var x = 5;
 var y = 5;
 var z = 6;
 (x == y)       // Returns true
 (x == z)       // Returns false