Math.sqrt i.e sqrt is a part of Math namespace.
// 2 ways to get square root.
Math.sqrt(100); // 10, Method 1
100*(1/2); // 10, Method 2
8*(1/3); // 2, works for cubic root also
Math.max() & Math.min():
Math.max(23,54,12,6,32,98,87,34,11); // 98
// Does type coercion also
Math.min(23,54,12,'6',32,98,87,34,11); // 6
// Does not do parsing
Math.min(23,54,12,'6px',32,98,87,34,11); // NaN
Inbuilt constants on Math object:
Math.PI * (Number.parseFloat('10px')**(2)); // Getting area
Generate a no b/w 1-6:
Math.trunc(Math.random() * 6) + 1;
Generatate a random no b/w an upper-lower limit:
const randomInt = (min, max) => Math.floor(Math.random() * (max-min)) + 1 + min;
randomInt(10,20);
// All of these Math.method() do type coercion.
Math.trunc(25.4); // 25
Math.round(25.4); // 25
Math.floor(25.4); // 25
Math.ceil(25.4); // 26
Math.floor is a better choice for negative numbers.
Math.trunc(-25.4); // -25
Math.floor(-25.4); // -26
// Rounding decimals: .toFixed returns a string, not a number
(2.5).toFixed(0); // '3'
(2.5).toFixed(3); // '2.500'
(2.345).toFixed(2); // '2.35'
// Add a unary + sign to convert it to a no.
+(2.345).toFixed(2); // 2.35
// Number is a primitive, hence they don't have methods. SO behind the scene, JS will do boxing, i.e transform primitive into a no object, perform the operation and then when operation is finished, transform it back to primitive.
Modular or Remainder Operator:
5 % 2; // 1
8 % 3; // 2
8 / 3; // 2.6666666666666665
// Odd or Even
const isEven = n => n%2 === 0;
isEven(20);
isEven(21);
isEven(22);
Usecase: Used to work with all odd rows, even rows, nth time etc.
Numeric Separators: [ES2021]
Used for representing really large numbers
These are underscores which can be placed between numbers. The engine ignores these underscores, its reduces the confusion for devs.
Ex. const diameter = 287_460_000_000;
diameter; // 287460000000
const price = 342_25;
price; // 34225
const fee1 = 1_500;
const fee2 = 15_00;
fee1 === fee2; // true
Underscore can be placed ONLY between numbers.
It cannot be placed adjacent to a dot of decimal.
It also cannot be placed at the begining or the end of the no.
const PI = 3.14_15;
PI; // 3.1415
All are invalid example of numeric separators
const PI = 3.1415; // Cannot be placed in the begining.
const PI = 3.1415; // Cannot be placed in the end.
const PI = 3_.1415; // Cannot be placed adjacent to a decimal dot.
const PI = 3.1415; // Cannot be placed adjacent to a decimal dot.
const PI = 3._1415; // Two in a row cannot be placed.
Converting Strings to Numbers:
Number('2500'); // 2500
Number('25_00'); // NaN , Hence we can only use when directly numbers are assigned to a variable. Hence, if a no is stored in the string or getting a no from an API, then to avoid error don't use '_' numeric separator.
Similar goes for parseInt i.e anything after _ is discarded as shown below:
parseInt('25_00'); // 25
BigInt
Special type of integers, introduced in ES2020
Numbers are represented internally as 64 bits i.e 64 1s or 0s to represent any number. Only 53 are used to store the digits, remaining are used to store the position of decimal point and the sign. Hence, there is a limit on the size of the number i.e ((2*53) - 1). This is the biggest no which JS can safely represent. The base is 2, because we are working in binary form while storing.
2*53 - 1; // 9007199254740991
Number.MAX_SAFE_INTEGER; // 9007199254740991
Anything larger than this is not safe i.e it cannot be represented accurately. Precision will be lost for numbers larger than this as shown in last digit. Sometimes it might work, whereas sometimes it won't.
Number.MAX_SAFE_INTEGER + 1; // 9007199254740992
Number.MAX_SAFE_INTEGER + 2; // 9007199254740992
Number.MAX_SAFE_INTEGER + 3; // 9007199254740994
Number.MAX_SAFE_INTEGER + 4; // 9007199254740996
Incase we get a larger no from an API larger than this, then JS won't be able to deal with it. So to resolve the above issue, BigInt a new primitive data type was introduces in ES2020. This can store integers as large as we want.
An 'n' is added at the end of the no to make it a BigInt. Ex.
const num = 283891738917391283734234324223122313243249821n;
num; // 283891738917391283734234324223122313243249821n
BigInt is JS way of displaying such huge numbers.
Another way using Constructor Fn for creating BigInt number.
const x = BigInt(283891738917391283734234324223122313243249821);
x; // 283891738917391288062871194223849945790676992n
Operations: All arithmetic operators work the same with BigInt;
const x = 100n + 100n;
x; // 200n
const x = 10n * 10n;
x; // 100n
Avoid mixing BigInt numbers with regular numbers
const x = 100n;
const y = 10;
z = x*y; // Error
To make it work, use BigInt constructor Fn:
z = x * BigInt(y);
z; // 1000n
Exception to it are comparsion operators & unary + operator.
20n > 19; // true
20n === 20; // false, === prevents JS from doing type coercion. Both the LHS & RHS have different primitive types, hence results in 'false'.
typeof 20n; // 'bigint'
typeof 20; // 'number'
20n == 20; // true, as JS does type coercion to compare only the values and not the types by converting BigInt to a regular number.
Same goes for this also: 20n == '20'; // true
Exception:
BigInt number is not converted to string on using + operator.
const num = 248923874328974239473829n
"num is huge i.e. " + num; // 'num is huge i.e. 248923874328974239473829'
Note:
Math.sqrt doesn't work with BigInt.
During division of BigInts, it discards the decimal part.
10 / 3; // 3.3333333333333335
10n / 3n; // 3n
12n / 3n; // 4n
This new primitive type adds some new capabilities to JS language to make it work with huge no.
以上是Math Namespace & BigInt的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript是现代网站的核心,因为它增强了网页的交互性和动态性。1)它允许在不刷新页面的情况下改变内容,2)通过DOMAPI操作网页,3)支持复杂的交互效果如动画和拖放,4)优化性能和最佳实践提高用户体验。

C 和JavaScript通过WebAssembly实现互操作性。1)C 代码编译成WebAssembly模块,引入到JavaScript环境中,增强计算能力。2)在游戏开发中,C 处理物理引擎和图形渲染,JavaScript负责游戏逻辑和用户界面。

JavaScript在网站、移动应用、桌面应用和服务器端编程中均有广泛应用。1)在网站开发中,JavaScript与HTML、CSS一起操作DOM,实现动态效果,并支持如jQuery、React等框架。2)通过ReactNative和Ionic,JavaScript用于开发跨平台移动应用。3)Electron框架使JavaScript能构建桌面应用。4)Node.js让JavaScript在服务器端运行,支持高并发请求。

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1
功能强大的PHP集成开发环境