JavaScript Example
- Previous Page JS D3.js
- Next Page JS HTML DOM
What Can JavaScript Do?
Where to Insert JavaScript
JavaScript Output
JavaScript Syntax
- JavaScript Statements
- JavaScript Numbers
- JavaScript Strings
- JavaScript Variables
- JavaScript Operators
- JavaScript Assignment
- JavaScript Expression (Using Constants)
- JavaScript Expression (Using Strings)
- JavaScript Expression (Using Variables)
- JavaScript Keywords
- JavaScript Comments
- JavaScript Is Case-Sensitive
JavaScript Statements
JavaScript Comments
JavaScript Variables
- JavaScript Variables
- JavaScript Variables as Algebra
- JavaScript Numbers and Strings
- JavaScript var Keyword
- Declaration of Multiple Variables in a Single Statement
- Multi-line Declaration of Multiple Variables
- Variables Without a Value Return undefined
- Re-declaring a Variable Does Not Destroy the Value
- JavaScript Array Addition
- JavaScript String Addition
- Addition of Strings and Numbers
- Addition of Numbers and Strings
JavaScript Arithmetic
JavaScript Assignment
JavaScript string concatenation
JavaScript Data Types
JavaScript Objects
- Create JavaScript Variable
- Create JavaScript Object
- Creating a person object (single line)
- Creating a person object (multi-line)
- Accessing object properties using .property
- Accessing object properties using [property]
- Accessing function properties using method access
- Accessing function properties using property access
JavaScript Functions
JavaScript Events
JavaScript Strings
- Strings can be enclosed in single or double quotes
- Show some string examples
- Adding a backslash before the quotation mark is recognized as a quotation mark
- Determining the length of the string
- Line breaks in text strings can be achieved using backslashes
- Backslashes cannot be used for line breaks in code
- Determining the first occurrence position of the text in the string - indexOf()
- Searching for text in the string and returning this text after finding it - match()
- Replacing characters in the string - replace()
- Converting the string to uppercase - toUpperCase()
- Converting the string to lowercase - toLowerCase()
- Splitting the string into an array - split()
JavaScript Numbers
- Numbers can be written with or without a decimal point
- Numbers that are too large or too small can be written using exponential notation
- Numbers are rounded to 15 digits
- Floating-point arithmetic is not always 100% precise
- However, this problem can be solved by multiplying and dividing by 10
- Adding two numbers will produce a new number
- Adding two number strings will produce a concatenated string
- Adding a number and a number string will also produce a concatenated string
- Adding a number string and a number will also produce a concatenated string
- Common mistakes when adding strings and numbers 1
- Common Error 2: Adding a string and a number
- JavaScript will try to convert strings to numbers in division operations
- JavaScript will try to convert strings to numbers in multiplication operations
- JavaScript will try to convert strings to numbers in subtraction operations
- JavaScript will not convert strings to numbers in addition operations
- The number divided by a string is no longer a number
- Dividing a number by a number string results in a number
- The global JavaScript function isNaN() determines whether a value is a number
- Using NaN in mathematical operations will always return NaN
- Using NaN in mathematical string operations will concatenate NaN
- The type of NaN is number (no, typeof NaN returns number)
- If a number outside the maximum possible number is calculated, it will return infinity (Infinity)
- Division by zero will generate Infinity
- Infinity is also a number (typeof Infinity returns number)
- Constants prefixed with 0x are interpreted as hexadecimal
- The toString() method can output numbers in hexadecimal, octal, or binary
- Numbers can be objects
- Numbers and objects cannot be safely compared
- Objects and objects cannot be safely compared
JavaScript Number Methods
- The toString() method converts a number to a string
- The valueOf() method returns a number as a number
- The toExponential() method returns a number in exponential notation
- The toFixed() method rounds a number to a specified number of digits
- The toPrecision() method returns a number of a specified length
- The global method Number() converts a variable to a number
- The global method Number() can even convert dates to numbers
- The global method parseInt() converts a string to a number
- The global method parseFloat() converts a string to a number
- MAX_VALUE returns the largest possible number in JavaScript
- MIN_VALUE returns the smallest possible number in JavaScript
- POSITIVE_INFINITY represents infinity
- Returns POSITIVE_INFINITY when overflow occurs
- NEGATIVE_INFINITY represents negative infinity
- Returns NEGATIVE_INFINITY when overflow occurs
- NaN stands for 'Not a Number'
- Arithmetic performed on a string will result in NaN
- Using numeric properties on a variable will return undefined
JavaScript Mathematics
- Math.PI returns the value of PI
- Math.round(x) returns the rounded value of x (rounding value)
- Math.pow(x, y) returns x to the power of y
- Math.sqrt(x) returns the square of x
- Math.abs(x) returns the absolute value of x
- Math.ceil(x) returns the upward rounded value of x
- Math.floor(x) returns the downward rounded value of x
- Math.sin(x) returns the sine of angle x (in radians)
- Math.cos(x) returns the cosine of angle x (in radians)
- Math.max() returns the highest value in the parameter list
- Math.min() returns the lowest value in the parameter list
- Convert Celsius to Fahrenheit
JavaScript Random
- Math.random() returns a random integer between 0 (inclusive) and 1 (exclusive)
- How to return a random integer between 0 and 9 (both inclusive)
- How to return a random integer between 0 and 10 (both inclusive)
- How to return a random integer between 0 and 99 (both inclusive)
- How to return a random integer between 0 and 100 (both inclusive)
- How to return a random integer between 1 and 10 (both inclusive)
- How to return a random integer between 1 and 100 (both inclusive)
- How to return a random integer between x (inclusive) and y (exclusive)
- How to return a random integer between x and y (both inclusive)
JavaScript Date
- Use Date() to display today's date and time
- Use getFullYear() to display the year
- Use getTime() to calculate the number of milliseconds since 1970
- Use setFullYear() to set a specific date
- Use toUTCString() to convert today's date (based on UTC) to a string
- Use getDay() to display the name of the week as a number
- Use getDay() along with an array to display the name of the week in order
- Display the clock
JavaScript Arrays
JavaScript Array Methods
- Add elements to an array
- Remove the last element from an array - pop()
- Join all elements of an array into a single string - join()
- Concatenate two arrays - concat()
- Concatenate three arrays - concat()
- Add an element to an array at position 2 - splice()
- Convert the array to a string - toString()
- Add new elements to the beginning of the array - unshift()
- Remove the first element from the array - shift()
- Select elements from the array - slice()
JavaScript Array Sorting
- Sort the array in ascending order
- Sort the array in descending order
- Sort numbers in ascending order
- Sort numbers in descending order
- Sort numbers (alphabetically or numerically)
- Sort the numbers in the array in random order
- Determine the smallest number in the array
- Determine the largest number in the array
- Use Math.min() to determine the smallest number in the array
- Use Math.max() to determine the largest number in the array
- Use the "self-made" myArrayMin method
- Use the "self-made" myArrayMax method
- Sort objects by numeric properties
- Sort objects by string properties
JavaScript Array Iteration
JavaScript Type Conversion
JavaScript Boolean
- Display the value of Boolean(10 > 9)
- Display the value of 10 > 9
- Everything that has a true value is true
- The boolean value of zero is false
- The boolean value of minus zero is false
- The boolean value of an empty string is false
- The boolean value of undefined is false
- The boolean value of null is false
- The boolean value of false is false
- The boolean value of NaN is false
JavaScript Comparison
- Assign 5 to x, then display the value of (x == 8)
- Assign 5 to x, then display the value of (x == 5)
- Assign 5 to x, then display the value of (x === 5)
- Assign 5 to x, then display the value of (x === "5")
- Assign 5 to x and then display the value of (x != 8)
- Assign 5 to x and then display the value of (x !== 5)
- Assign 5 to x and then display the value of (x !== "5")
- Assign 5 to x and then display the value of (x > 8)
- Assign 5 to x and then display the value of (x < 8)
- Assign 5 to x and then display the value of (x >= 8)
- Assign 5 to x and then display the value of (x <= 8)
JavaScript Conditions
JavaScript Loops
JavaScript Error Handling
JavaScript Regular Expressions
JavaScript Objects
- Create JavaScript Variable
- Create JavaScript Object
- Create JavaScript Object (Single-line)
- Create JavaScript Object (Multi-line)
- Create JavaScript Objects Using new
- Create JavaScript Objects Using Constructors
- Create Built-in JavaScript Objects
- The Best Way to Create JavaScript Variables
- JavaScript Objects Are Mutable
JavaScript Object Properties
JSON Object
JSON Array
JSON Parsing
JSON Stringify
JSON PHP
JSON HTML
JSON JSONP
- Previous Page JS D3.js
- Next Page JS HTML DOM