JavaScript Math functions examples
List of usefull JavaSript functions and constans
var 1_radian_to_degree = 180 / Math.PI; // 57,295779513082320876798154814105 degree
var 1_degree_to_radian = Math.PI / 180; // 0,01745329251994329576923690768489 radian
var 360_degree = 2 * Math.PI * 1_radian_to_degree;
parseInt("10.33") // returns 10 string to integer
parseFloat("10.33") // returns 10.33 string to float
// change sign
var a = -1;
a = a * -1; // +1
// random sign
a *= Math.floor(Math.random()*2) == 1 ? 1 : -1;
var who_is = 58;
var type = (typeof who_is); // number
Math.E // natural logarithms, e, approximately 2.718281828459045
Math.LN10 // natural logarithm of 10, approximately 2.302585092994046
Math.LN2 // natural logarithm of 2, approximately 0.6931471805599453
Math.PI // approximately 3.141592653589793
Math.SQRT1_2 // square root of 1/2 which is approximately 0.7071067811865476
Math.SQRT2 // square root of 2, approximately 1.4142135623730951
Math.pow(4, 2) = 16 // 4 x 4 ... base 4 to the exponent 2 power
Math.abs(-5) absolute value of a number // return 5
Math.tan(degrees * Math.PI / 180) // get tangent from degrees
Math.tan(degree * Math.PI / 180)*adjacent = opposite
opposite / Math.tan(degree * Math.PI / 180) = adjacent
Math.acos(adjacent / hypotenuse) // angle of a right-angle triangle in radians
Math.atan(opposite / adjacent) // angle of a right-angle triangle in radians
var adjacent = Math.cos(radian) * hypotenuse; // leng of adjacent side
// find the point on a circle if you know the circle´s radius
var x = Math.cos(pointAngleInRadiansFromCentreOfCircle) * radius;
var y = Math.sin(pointAngleInRadiansFromCentreOfCircle) * radius;
Math.cbrt(64) // cube root of a number 4 (4 x 4 x 4)
Math.exp(xNumber) // returns e<sup>xNumber</sup> ... e == Math.E
Math.ceil() // always rounds a number up to the next largest integer
Math.ceil(1.001) // 2
Math.floor() // returns largest integer less than or equal to a given number
Math.floor(5.85) = 5
Math.floor(-5.15) = -6
const arrayA = [2, 3, 1];
Math.min(...arrayA) = 1;
Math.max(1, 3, 2) = 3;
Math.random() // returns 0.000.... - 0.99999999999
Math.random() * 3 // returns 0.000 - 2.99999999999
Math.round() // returns value of a number rounded to the nearest integer.
Math.round(0.9) // 1
Math.round(5.5) // 6
Math.round(5.05) // 5
Math.floor(Math.random() * 3) // returns 0, 1, 2
Date: 14.06.2020 - 08:3877LW NO topic_id
AD
Další témata ....(Topics)
toString([radix])
/* prevede objekt na string */
toLocaleString()
var myDate = new Date();
document.write( myDate.toLocaleString());
// 24. února 2009 10:50:02
valueOf()
var number = new Number();
number = Number(100);
if (!isNaN(number))
document.write(number.valueOf()); // 100
hasOwnProperty(proName)
var theObj = { prom : 'bla' };
if ( theObj.hasOwnProperty )
{
document.write( "Has 'prom': " + theObj.hasOwnProperty( 'prom' ) + 'n' ); // true
document.write( "Has 'prom2': " + theObj.hasOwnProperty( 'prom2' ) + 'n' ); // false
}
isPrototypeOf(object2)
function fc(){
var reg = new RegExp();
return (RegExp.prototype.isPrototypeOf(reg)); // return true.
}
propertyIsEnumerable(proName)
var a = new Array("apple", "banana", "cactus");
document.write(a.propertyIsEnumerable(2)); // true
HTML TAGS EXAPLES
ANCHOR:
<a href="#bla">bla target</a>
invisible target
<a name="bla">bla</a>
OR visible target
<h2 id="bla"></h2>
JavaScript
var elmnt = document.getElementById("bla");
elmnt.scrollIntoView();
Invisible hidden collabsible toggle switch DIV
<div id="idDiv">Hello</div>
// javascript button onclick
if(show){
idDiv.style.display === "block";
}else{
idDiv.style.display = "none";
}
Date: 16.07.2020 - 10:27
Jak vybrat kód mezi taky v Notepad++
How to select f.e. javascript tag contetn in Notepad++
- insert content between braces
- put caret behind the last bracket
- press Ctrl + left Alt + B or Ctrl + Shift + B to control the first brace
from parentheses to braces and check the multi-line box.
Then you can Ctrl-double-click on either brace to highlight everything in between.
How to select f.e. javascript tag contetn in Notepad++
- insert content between braces
- put caret behind the last bracket
- press Ctrl + left Alt + B or Ctrl + Shift + B to control the first brace
< script>
{
var bla = 0;
// bla bla ... long content
} // place caret/cursor behind the last brace { and press Ctrl + left Alt + B (Notepad++)
< /script>
At Settings->Preferences->Delimiter, change the delimiters
from parentheses to braces and check the multi-line box.
Then you can Ctrl-double-click on either brace to highlight everything in between.
Pokud vytvářite novou instanci nějakého objektu, musíte použít klíčové slovo new
class myClass {
private $var = 'Who I am? ';
function __construct($val){
$this->var .= $val;
}
function fc()
{
print 'Hello from myClass <br>';
print $this->var;
}
}
$myInstance = new myClass('I am a variable');
$myInstance->fc(); // Hello from myClass <br>Who I am? I am a variable
Editace: 20.6.2020 - 08:32
Počet článků v kategorii: 77
Url:javascript-math-functions-constans-examples