math, string, operator, array
in Programming on Javascript
Javascript Basic
Javascript Math Object
The JavaScript Math object allows you to perform mathematical tasks on numbers.
If we know the existence of these kinds of function, we can save time.
// Example
Math.PI // returns PI
Math.round() // returns the value of x rounded to its nearest integer:
Math.max(0, 150, 30, 20, -8, -200); // returns 150
Math.min(0, 150, 30, 20, -8, -200); // returns -200
You can find information in detail below.
Javascript Math Object
String
Expressions - typeof
console.log(typeof 42); // expected output: "number"
console.log(typeof 'blubber'); // expected output: "string"
console.log(typeof true); // expected output: "boolean"
console.log(typeof declaredButUndefinedVariable); // expected output: "undefined";
You can find information in detail below.
Reference typeof
Expressions - String Methods
// String Length
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
// Result : 26
// Finding a String in a String
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
// Result : 7
// Slice() Method
var str = "Apple, Banana, Kiwi";
var res = str.slice(7, 13);
// Result : Banana
You can find information in detail below.
JavaScript String Methods
Equals operator
Difference between “==” and “===”
The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done,
and the types must be the same to be considered equal.
alert(null == undefined); //true
alert(null === undefined); //false
// because null and undefined are different type.
alert(true == 1); //true
alert(true === 1); //false
alert(true == '1'); //true
alert(true === '1'); //false
alert(0 === -0); //true
alert(NaN === NaN); //false
You can find information in detail below.
Javascript equality table
Array
ADD
Push() (add one data)
var li = ['a', 'b', 'c', 'd'];
li.push('f');
Concat() (add multiple data)
var li = ['a', 'b', 'c', 'd'];
li.concat('f','g');
unshift() (add to the front)
var li = ['a', 'b', 'c', 'd'];
li.unshift('z');
splice() (select the point that you want to put)
var li = ['a', 'b', 'c', 'd'];
li.splice(2, 0, 'b');
DELETE
Shift() (delete one data from the front)
var li = ['a', 'b', 'c', 'd'];
li.unshift();
pop() (delete on date from the last)
var li = ['a', 'b', 'c', 'd'];
li.pop();
SORT
sort()
var li = ['d', 'c', 'b', 'a'];
li.sort();