Posts

The math object part 2

Image
Math Object Methods The  Math   object  contains a number of methods that are used for calculations: For example, the following will calculate the  square root  of a number. var number = Math . sqrt (4); document.write(number); //Outputs 2 Try It Yourself To get a random number between 1-10, use  Math .random(), which gives you a number between 0-1. Then multiply the number by 10, and then take  Math .ceil() from it:  Math .ceil( Math .random() * 10).

The Math Object part 1

Image
COURSES CODE PLAYGROUND DISCUSS TOP LEARNERS The Math Object 47 1/3           The Math Object The  Math   object  allows you to perform mathematical tasks, and includes several properties. For example: document.write( Math .PI ); //Outputs 3.141592653589793 Try It Yourself Math  has no  constructor . There's no need to create a  Math   object  first. 133  

Associative Arrays part 2

Associative Arrays Remember that JavaScript  does not  support arrays with named indexes. In JavaScript, arrays always use numbered indexes. It is better to use an  object   when you want the index to be a  string  (text). Use an  array   when you want the index to be a  number . If you use a named index, JavaScript will redefine the  array  to a standard  object .

Associative Arrays part 1

Image
COURSES CODE PLAYGROUND DISCUSS TOP LEARNERS Associative Arrays 46 1/2       Associative Arrays While many programming languages support arrays with named indexes (text instead of numbers), called  associative arrays , JavaScript  does not . However, you still can use the named  array syntax, which will produce an  object . For example: var person = []; //empty array person["name"] = "John"; person["age"] = 46; document.write(person["age"]); //Outputs "46" Try It Yourself Now, person is treated as an  object , instead of being an  array . The named indexes "name" and "age" become properties of the person  object . As the person  array  is treated as an  object , the standard  array  methods and properties will produce incorrect results. For example,  person.length  will return 0.

Array Properties & Methods part 2

Combining Arrays JavaScript's  concat()   method  allows you to join arrays and create an entirely new  array . Example: var c1 = ["HTML", "CSS"]; var c2 = ["JS", "C++"]; var courses = c1.concat(c2) ; Try It Yourself The  courses  array  that results contains 4 elements (HTML, CSS, JS, C++). The  concat  operation does not affect the  c1  and  c2  arrays - it returns the resulting concatenation as a new  array .

Array Properties & Methods part 1

The length Property JavaScript arrays have useful  built-in properties and methods. An  array 's  length  property returns the number of it's elements. var courses = ["HTML", "CSS", "JS"]; document.write( courses.length ); //Outputs 3 Try It Yourself The  length  property is always one more than the highest  array  index. If the  array  is empty, the length property returns  0 .

Other way to create array part 2

Image
RSES CODE PLAYGROUND DISCUSS TOP LEARNERS Other Ways to Create Arrays 40 2/2       Creating Arrays JavaScript arrays are dynamic, so you can declare an  array  and not pass any arguments with the Array()  constructor . You can then add the elements dynamically. var courses = new Array () ; courses[0] = "HTML"; courses[1] = "CSS"; courses[2] = "JS"; courses[3] = "C++"; Try It Yourself You can add as many elements as you need to. Array Literal For greater simplicity, readability, and execution speed, you can also declare arrays using the  array  literal  syntax. var courses = ["HTML", "CSS", "JS"]; Try It Yourself This results in the same  array  as the one created with the  new Array()  syntax.  You can access and modify the elements of the  array  using the index number, as you did before. The  array  literal  syntax is the recommended way...