JavaScript Syntax Practice

Previous: JavaScript Intro and JavaScript Functions.

Use the Console to practice some useful JavaScript methods and built-in functions. Learn to differentiate between them. When are there parentheses? What goes inside them?

.length
Property.
Usage: varname.length;
Returns the number of characters (including spaces and punctuation) in the string, or in the case of an array, the number of items in the array. This is a PROPERTY, so there are no parentheses. var words = "Hello, World!";
words.length;
var list = ["red", "white", "blue"];
list.length;
.substring()
Method.
Usage: varname.substring(3);
Also: varname.substring(3, 5);
Returns a string that begins with index 3 in varname and continues either to the end of varname or to the second index supplied. Note that the first character in a string has an index of 0 (just like the first item in an array). var wise = "Eat more fruit today.";
wise.substring(4);
wise.substring(9, 14);
.toString()
Method.
Usage: varname.toString();
Returns a string representing the specified Number object. var social = 321894567;
var newSocial = social.toString();
console.log(social + " and " + newSocial);
social === newSocial;
parseInt()
Function.
Usage: parseInt(varname, 10);
If varname is a string containing a number or numbers, an integer is returned. (10 means "base 10." Other bases may be used.) If varname cannot be converted, the result will be NaN (which means "not a number").
This will return NaN: parseInt("Hello", 10);
NOTE: If varname is a string containing a number with decimal places, use parseFloat() to return a float. Otherwise, everything after the decimal will be lost. Try this:
var age = prompt("What is your age? ");
var newAge = age + 10;
alert("In 10 years you will be " + newAge + " years old.");
Not the answer you expected? Try this:
newAge = parseInt(age, 10) + 10;
alert("In 10 years you will be " + newAge + " years old.");
isNaN()
Function.
Usage: isNaN(varname);
Returns true if varname is NOT a number. var earth = "round";
var circle = 360;
isNaN(earth);
isNaN(circle);
Math.random()
Function.
Usage: var num = Math.random();
That will return a value such as 0.43021444068290293 -- a number between 0 and 1.
var randy = Math.floor(Math.random() * 6) + 1;
Without + 1, you would get random numbers from 0 to 5 (total of 6 possibilities). With + 1, you get random numbers from 1 to 6 -- which is like rolling a die. Random numbers are generated in games to determine all kinds of qualities, such as health or strength. Usually there is a top and a bottom limit. (See below for .floor vs. .round. When generating random numbers for gameplay, .floor provides a more uniform distribution than .round.) var number = Math.floor(Math.random() * 100) + 1;
var guess = prompt("Type a number between 1 and 100. ")
guess = parseInt(guess, 10);
if (guess < number) { confirm("Your guess is too low. Try again?") };
if (guess > number) { confirm("Your guess is too high. Try again?") };
Math.floor()
Function.
Usage: Math.floor(41.9999); // returns 41
Returns the largest integer less than or equal to a given number. Thus it never "rounds up," but always "down." var num = 4.9;
var newNum = Math.floor(num);
console.log(num + " becomes " + newNum);
Math.round()
Function.
Usage: Math.round(41.9999); // returns 42
Usage: Math.round(41.4999); // returns 41
Returns the value of a number rounded to the nearest integer. var nums = 4.9;
var newNums = Math.round(nums);
console.log(nums + " becomes " + newNums);

Published under an MIT License. Copyright © 2015 Mindy McAdams. Fork on GitHub.