Vous êtes sur la page 1sur 4

The <script> Tag

Example
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>

External JavaScript
Scripts can also be placed in external files:

myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}

Example
<script src="myScript.js"></script>

JavaScript Display Possibilities


JavaScript can "display" data in different ways:

Writing into an alert box, using window.alert().

Writing into the HTML output using document.write().

Writing into an HTML element, using innerHTML.

Writing into the browser console, using console.log().

Declaring (Creating) JavaScript Variables


You can declare a JavaScript variable with or without the var keyword:

Example
carName = "Volvo";
var person = "John Doe", price = 200;

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:

Example
carName = "Volvo";
var carName;

JavaScript Data Types


In JavaScript there are 5 different data types that can contain values:

string

number

boolean

object

function

There are 3 types of objects:

Object

Date

Array

And 2 data types that cannot contain values:

null

undefined

JavaScript Type Conversion

Number() converts to a Number, String() converts

to a String, Boolean() converts to a Boolean.

JavaScript Operators
Operator

Description

On numbers : addition | On strings : concatenation

Subtraction

Multiplication

Division

Modulus

++

Increment

--

Decrement

JavaScript Assignment Operators


Operator

Example

Same As

x=y

x=y

+=

x += y

x=x+y

-=

x -= y

x=x-y

*=

x *= y

x=x*y

/=

x /= y

x=x/y

%=

x %= y

x=x%y

JavaScript String Methods and Properties


Method / property

Description

length

Returns the length of a string

indexOf ( str )

Returns the position of the first occurrence of the specified text (str) in a string

lastIndexOf ( str )

Returns the position of the last occurrence of the specified text (str) in a string

substring (start, end )

Returns a part from a string starting from start to end

substr ( start, L )

Returns a part from a string that contains L characters starting from start

replace ( S, R )

Replaces S by R and returns the resulting string

toUpperCase ()

Converts to uppercase and returns the resulting string

toLowerCase ()

Converts to lowercase and returns the resulting string

charAt ( pos )
split ( delimiter )

Returns the character at a specified index ( pos ) in a string


Splits a string by delimiter and returns the result in an array

Number Methods and Properties


Method / property

Description

toString ()

Returns a number as a string

Number (str)

Returns variable str as a number if possible, NaN otherwise

parseFloat (str)

Returns the first number in str as integer or float, returns NaN if no number has
been found at the beginning

parseInt ( str )

Returns the first number in str as integer, returns NaN if no integer has been
found at the beginning

Math.abs ( X )

Returns the absolute value of X

Math.round ( X )

Returns X, rounded to the nearest integer

Math.ceil ( X )

Returns X, rounded upwards to the nearest integer

Math.floor ( X )

Returns X, rounded downwards to the nearest integer

Math.min ( a, b, c, d, )

Returns the number with the lowest value

Math.max ( a, b, c, d, )

Returns the number with the highest value

Vous aimerez peut-être aussi