Data types, String and Math functions in JavaScript

Data types, String and Math functions in JavaScript

Data types in JavaScript

var years = 3; 											// number
var name = 'Codecrumbs'; 								// string
var name = { first: "string A", last: "string B" }; 	// object
var isValid = false; 									// boolean
var arrayA = ['apple', true, 2]; 						// array
var b; typeof b; 										// undefined
var b = null; 											// value null

String Functions

const string = "My example hello world";

string.indexOf("example"); // 3

string.lastIndexOf("hello"); // 11

string.replace("example","exam:"); // My exam: hello world

string.toUpperCase(); // 'MY EXAMPLE HELLO WORLD'

string.toLowerCase(); // 'my example hello world'

string.concat(" ","new string"); // 'My example hello world new string'

encodeURI(string); // 'My%20example%20hello%20world'

decodeURI(string); // 'My example hello world'

Math Functions

Math.round(4.7); // 5
Math.round(4.3); // 4
Math.pow(3,3); // 27
Math.sqrt(64); // 8
Math.min(-9,1,2,5); // -9
Math.max(-9,1,2,5); // 5
Math.random; // Random number between 0 and 1

Read more