Some sample JavaScript code snippets for:
- Declaring numeric variables in JavaScript and using typeof and IsNaN() to test that a variable contains a number which can be used in calculations.
- Perform addition, subtraction, multiplication, and division on numeric variables.
- Apply operator precedence in arithmetic calculations.
- Use methods of the JavaScript Math object to:
- Manipulate positive and negative numbers (Math.abs(n) and Math.sign(n))
- Work with integers and floats (Math.round(n) and Math.trunc(n))
- Find maximum and minimum values (Math.max(x,y,z) and Math.min(x,y,z))
- Calculate ceiling and floor values (Math.ceil(n) and Math.floor(n))
- Generate random numbers (Math.random()).
Instructions for use
- the web page containing the JavaScript code.
- Open the page in Brave or other, lesser web browsers.
- View the JavaScript output by pressing the following keys and selecting the Console tab:
- Ctrl + Shift + i (Windows)
- ⌘ Command + Shift + i (Mac).
- Open the web page in VS Code, VS Code Online or Sublime Text. All are good choices.
- Play around with the JavaScript code. Change things and see what happens.
✅ That’s it.