- Briefly describe the history of JavaScript and its purpose as a language
- Define JavaScript variables and data structures
- Utilize primitives and operators in order to solve problems using JavaScript
- Manipulate integers, floating point, and decimal numbers
- Manipulate strings
- Use booleans as return values, as variables, and in the context of a conditional
Then
- developed by Brendan Eich (for Netscape, now Mozilla)
- 10 days
- went through a few different names before settling on JavaScript (Java was popular)
- taken to ECMA for standardization
Now
- JavaScript is THE front-end language
- clunky, things to complain about, but it works and is web-driven!
- tons of open source libraries
- also used as a backend language, using Node.js
- working on ECMAScript 6
Repl.it is a quick and easy tool for practicing Javascript (and other languages!) that we'll use throughout the class. Here is a Repl.it with the code from this lesson you can reference later, but for now open up a new repl of your own to code along to this lesson in!
Comments come in two forms
// descriptive stuff
/**
These
are
comments on
many lines
*/
Numbers are one of the types of values we want to be able to interact and play with in JS.
..., -1, 0, 2, 3, 4, 5, ...
2.718, 3.14, .5, .25, etc
In JS these are both the same type of object, which it calls Numbers.
This can infrequently cause problems!
0.1 * 0.2 = 0.020000000000000004
How to deal with floating point precision in JavaScript
2 + 2 * 3
How would you get the 2 + 2
to execute before the * 3
? In other words, how would you change this expression to get 12?
Strings are collections of letters and symbols known as Characters, and we use them to deal with words and text in Javascript. Strings are just another type of value in Javascript.
"John"
'Jane'
You can use operators on strings too! Try typing "John" + "Jane"
. This is called String concatenation
[Type coercion is the process of converting value from one type to another](https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/#:~:text=Type coercion is the process,Symbol (added in ES6).)
Try this...
"1" + 1
Without removing the quotes, how would you get this to equal 2?
Booleans are a type that can only have one of two values: true or false.
true
false
+ (add)
- (subtract)
* (multiply)
/ (divide)
% (modulus)
Javascript can be a little cheap with the number of operations it allows you to do. For example, how is someone supposed to square a number or cube a number easily? Luckily there is a special Math
object with some very useful methods.
- Taking a number to some
power
? Then just useMath.pow
// 3^2 becomes
Math.pow(3, 2);
// => 9
// 2^4 becomes
Math.pow(2, 4);
// => 16
- Taking a square root
// √(4) becomes
Math.sqrt(4);
// => 2
- Need a
random
number? Then useMath.random
.
// The following only returns a random decimal
Math.random();
// => .229375290430
/**
The following will return a
random number between 0 and 10
*/
Math.random() * 10;
- Since Numbers can be Floats or Integers we often want to get rid of remaining decimal places, which can be done using
Math.floor
.
// Remove the decimal
Math.floor(3.14)
// => 3
Math.floor(3.9999)
// => 3
Having made some expressions it becomes evident we want to store these values.
var myNumber = 1;
// or also
var myString = "Greetings y'all!";
The main note to make here is that these variables should always have the var
keyword and use camelCase
Prior to ES6 JavaScript had one way of declaring variables: var
. Now there
are much better ways to declare variables: let
and const
. The basic rule of
thumb should be to use const
when you don't need to reassign that variable,
and let
when you do. You will continue to see var
in legacy code bases and
documentation online, but you shouldn't use in your own code.
// this is the modern way to declare variables
let myNum = 1
myNum++
const unchangeable = 'cannot change me'
// this would be a syntax error
// unchangeable = 'anything'
In Javascript we just discussed two types of values we can use. We call these values objects, which for now just means that in addition to storing some data you also get to use some helpful methods when you are working with them.
In JavaScript, almost everything is an object (primitive types are the exception).
- If you want to turn a number into a string you can use a helpful method called
toString
.
myNumber.toString()
// => "1"
- Numbers
.toString()
- converts a number to a string.toFixed()
, - converts a number to a fixed string representationparseInt('33')
- converts a string to an integerparseFloat('3.1')
- converts a string to a floating point number
- Strings
.split('')
- converts a string into an array split in a provided character.indexOf('s')
- returns the index of the first appearance of a provided string.toUpperCase()
- converts a string to all caps.toLowerCase()
- converts a string to all lowercase.replace('old', 'new')
- replaces the first appearance of a string with a new string
Note that most of these functions are called on an object, while functions like parseInt()
and parseFloat()
only take in arguments.