N.S.B. Cosmic Center |
||
JavaScript & MathScript Interactive Document Generator-Demo-02This document is generated by N.S.B. JavaScript & MathScript Interactive Document Generator version 001. Introduction to N.S.B. JavaScript & MathScript Interactive Document Generator JavaScript Data Types Now that you know how to input and execute JavaScript code, let us examine some data types and some associated manipulations. This sequence defines a number, examines and determines its type: input(1)=> 3.45output(1)=> 3.450input(2)=> n=$()output(2)=> 3.450input(3)=> typeof(n)output(3)=> numberinput(4)=> isInt(n)output(4)=> falseinput(5)=> isFlt(n)output(5)=> trueNotice that typeof() is a basic JavaScript function. On the other hand,the functions isInt() and isFlt() are defined by our program. The function isInt() checks to see whether the data type is an integer; the function isFlt() checks to see whether it is "float", which means it has a decimal point. There is another function isXpn(); this checks whether a number has exponential notation. Consider these: input(6)=> c=speedOfLight; G=constGravity; m=mass('sun')output(6)=> 1.989e+30Many of the physical constants (such as speedOfLight and constGravity) and related functions (such as input(7)=> isNum(c)output(7)=> trueinput(8)=> isInt(c)output(8)=> trueinput(9)=> isNum(G)output(9)=> trueinput(10)=> isInt(G)output(10)=> falseinput(11)=> isFlt(G)output(11)=> falseinput(12)=> isXpn(G)output(12)=> trueinput(13)=> isXpn(m)output(13)=> trueLet us verify the above statements by seeing our numbers. Putting them in a JavaScript array: input(14)=> aa=[c, G, m]output(14)=> [ 299792458, 6.673e-11, 1.989e+30 ]Hence, we see that Now that we have defined the above physical constants, let us compute the well-known Schwarzschild radius of the sun: input(15)=> 2*G*m/pow(c,2)output(15)=> 2953Notice that all our physical quantities are given in the MKS or SI system of units. Hence the above radius is given in meters. The followings give the above result in kilometers, in miles, and in centimeters: input(16)=> $(15)/kilooutput(16)=> 2.953input(17)=> $(15)/mileoutput(17)=> 1.835input(18)=> $(15)/centioutput(18)=> 2.953e+5We shall continue our exploration of JavaScript data type in the next document. Next |
||
Home |