JAVASCRIPT
Variable name rules
- Cannot start with a number - No spaces. - No dots(.) or dashes (-) - Should have descriptive names - the convention is to use camel casing in JavaScript
JavaScript distinguishing between data types
- Numbers (no distinction between real numbers and integers) - Strings (Can contain multiple words) - booleans JavaScript automatically gets the value UNDEFINED if it hasn't been given a value
getElementsByTagName
- Takes a string as a parameter - Returns a list of elements(objects) that have that tag. This is faster than querySelectorAll
getElementsByClassName
- Takes a string as a parameter - Returns a list of elements(objects) that have the class parameter
Methods to access the DOM nodes
- getElementById - querySelector - querySelectorAll - getElementsByClassName - getElementsByTagName - etc. they must be prefixed by: document.<method name>
What is Javascript?
...Javascript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images and more... It is an object oriented programming language that was initially only used for the web, mostly web browsers and most smart phone browsers support it as well.
console.log(var)
A builtin function to print values to the browser's console.
FUNCTION EXPRESSIONS
A function that is stored inside a variable. the function is called via the variable name.
Camel casing
A naming convention of writing compound words or phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation example: var myVariable = 3;
What is a Script?
A script is a set of instructions that a computer can follow in order to achieve a goal.
What is a scripting language?
A scripting language is a programming language that supports scripts: programs written for a special run-time-environment. Scripting languages are often interpreted (rather than compiled)
JavaScript ARRAYS
Arrays in JavaScript are objects. - They know their own size - They have builtin functions that are very helpful when working with arrays. - declared using brackets [ ]
DOM
Document Object Model It's a model of the HTML page, constructed by the browser. - constructed as a tree of objects. - - each HTML element is an object on the tree - The objects on the tree are often called nodes. - Javascript can be used to interact with the DOM tree.
Object oriented language
Everything in JavaScript is an object, even functions.
Control Flow in JavaScript
For loops and while loops work the same as in C++. if-else and switch work but the comparison is different as javascript allows both loose and strict comparison
Interpreted vs. compiled
If a program is compiled it will not execute if the compiler finds an error in the code. - Takes a short time to run - but if there is a mistake in the code you need to start all over again. - Faster performance by directly using the native code of the target machine - Opportunity to apply quite powerful optimisations during the compile stage Interpreted language only run one line at a time. The advantages: - Easier to implement (writing good compilers is very hard!!) - No need to run a compilation stage: can execute code directly "on the fly" - Can be more convenient for dynamic languages
LOOSE VS STRICT COMPARISON
Loose comparison uses type coercion that makes it possible for a string and a integer to valuate as true. Strict comparison makes sure that the values are of the same type and the contents match.
getElementById
Quickest way since no two elements may have the same id. takes a string as a parameter
querySelectorAll
Similar to querySelector but returns a list of all the elements that match.
querySelector
Takes a CSS selector as a parameter and return the first element that matches it.
Loosely typed language
The type doesn't have to be stated when a variable is created and the variable can change types quite easily. (see photo) Javascript is a loosely typed language that uses the keyword var is used for any type of variable.
JavaScript FUNCTIONS
They can be declared several ways: - using the function keyword. - if there is no return statement then the function automatically returns undefined. - NO return type in functions in JavaScript. - Functions can be treated like any other variable. - a function can be assigned to a variable. - a function can return a function - a function can be an argument to other functions.
EVENT DRIVEN PROGRAMMING VS PROCEDURAL PROGRAMMING
We have been learning procedural programming for C++. when executing we start at the top and end at the bottom. Event driven programming is what we use with javascript. A programmed functionality won't run until an event is triggered. e.g. a button is clicked. which is an event that triggers a javascript code.
Strictly typed language
the type of an variable has to be stated when a variable is created.