JavaScript - 语法
JavaScript Output
JavaScript does not have any built-in print or display functions.
JavaScript can “display” data in different ways:
writing into an alert box, using
window.alert()
writing into the html output using
document.write()
using document.write() after an html document is fully loaded, will delete all existing html. so, this method should only be used for testing.
writing into an html element, using
innerHTML
writing into the browser console, using
console.log()
JavaScript Syntax
JavaScript is case sensitive.
The JavaScript syntax defines two types of values: Fixed values and Variable values.
Fixed values are called literals. Variable values are called variables.
- Literals : number, string
- Varisble : use the var keyward to declare variables
JavaScript Variables
Re-Declaring JavaScript Variables
if you re-declare a javascript variable, it will not lose its value.
the variable carName will still have the value ‘Volvo’ after the execution of these statements
1 | var carName = "Volvo"; |
JavaScript Operators
- typeof : returns the type of a variable
- instanceof : returns true if an object is an instance of an object type
when adding a number and a string, javascript will treat the number as a string.
1 | var x = 16 + "volvo"; => var x = "16" + "volvo"; |
javascript evaluates expressions from left to right. different swquences can produce different results:
1 | var x = 16 + 4 + "volvo"; => 20volvo |
1 | var person; // value is undefined, type is undefined |
JavaScript Object
Do not declare strings, numbers, and booleans as object
avoid string, number, and boolean objects. they complicate your code and slow down execution speed.
1 | var x = new String(); // declares x as a string object |
JavaScript objects cannot be compared
1 | var x = new String("John"); |
JavaScript String
indexOf(), lastIndexOf(), search()
toUpperCase(), toLowerCase()
concat() : str1.concat(“ “, str2, str3);
split(“”) : a string can be converted to an array with the split() method
extracting string parts 字符串截取
1 | slice(start, [end]) : extracts a part of a string and returns the extracted part in a new string.可以接受参数是负值 |
- replacing string content 字符串替换
1 | replace(str1, str2) |
by default, the replace() function replaces only the first match. to replace all matches, use a reqular expression with a g flag.
the replace() method does not change the string it is called on. it returns a new string.
JavaScript Number
use the toString() method to output numbers as base 16(hex), base 8(octal), or base 2(binary).
1 | var myNumber = 128; |
- infinity
infinity is a number: typeOf infinity returns number
1 | var x = 2 / 0; // x will be infinity |
- NaN
NaN is a number, and typeof NaN returns number
1 | var x = 100 / 'apple'; // x will be NaN |
- toString() : return a number as a string
- valueOf() : retuan a number as a number
toExponential(number) : return a string, with a number dounded and written using exponential na=otation.
1
2
3
4var x = 9.656;
x.toExponential(2); // 9.66e+0 参数代表了小数位数
x.toExponential(4); // 9.6560e+0
x.toExponential(6); // 9.656000e+0toFixed() : return a string, with the number written with a specified number of decimals
1
2
3
4
5var x = 9.656;
x.toFixed(0); // 10
x.toFixed(2); // 9.66
x.toFixed(4); // 9.6560
x.toFixed(6); // 9,656000toPrecision() : return a string, with a number written with a specified length
1
2
3
4
5var x = 9.656;
x.toPrecision(); // 9.656
x.toPrecision(2); // 9.7
x.toPrecision(4); // 9.656
x.toPrecision(6); // 9.65600Number(), paseFloat(), paseInt()
1
2
3
4x = true; Number(x); // 1
x = false; Number(x); // 0
pasrInt("10.33"); // 10
parseFloat("10.33"); // 10.33
JavaScript Scope
- local variable
variables declared within a jacascript function, become locall to the function.
local variables have local scope: they can only be accessed within the function.
- global variable
a variable declared outside a function, becomes global.
a global variable has global scope: all script and function on a web page can access it.
- automatically global
1 | myFunction(); |
- global variables in html
with javascript, the global scope is the complete javascript environment.
in html, the global scope is the window object. all global variables belong to the window object.
1 | var carName = "Volvo"; |