var x; //defines the variable x, although no value is assigned to it by default
var y = 2; //defines the variable y and assigns the value of 2 to it
var z = "String";
function add(a, b) {
return a + b;
}
var c = add(2,3);
var text = "javascript is cool";
var a = text["length"];
var b = text.length;
var cat = {colour: "grey", name: "Bogdan"};
delete cat.colour;
var colour = cat.colour;
var rabbit = {};
rabbit.speak = function(line) { print("The rabbit says '", line, "'"); };
rabbit.speak("Well, now you're asking me.");
var txt = "string";
charAt() : Returns the character at the specified index indexOf() : Returns the position of the first found occurrence of a specified value in a string toLowerCase() : Converts a string to lowercase letters toUpperCase() : Converts a string to uppercase letters
var myCars=["Saab","Volvo","BMW"]; var name=myCars[0];
pop() : Remove the last element of an array
push() : Add new elements to the end of an array - push()
reverse() :Reverse the order of the elements in an array
function People(name, age) {
this.name = name;
this.age = age;
this.speak = function() {
return "I am a person";
};
}
var bogdan = new People("Bogdan", 23);
var andrei = new People("Andrei", 18);
bogdan.speak();
Each time a function is called with the new keyword in front of it "this" variable will point at a new object.
<script>
function myFunction() {}
</script>
<script src="myJsFile.js"></script>
window.alert("Hello"); Displays an alert box with the message "Hello" and an OK button
window.confirm("Confirm"); Displays a dialog box with a message and an OK and a Cancel button
window.close(); Closes the current window
document.write("Hello World!");
var a = "test";
document.write(a);
document.title; Return the title of the document;
document.getElementById(id)
document.getElementById(id).innerHTML = new HTML;
var htmlText = document.getElementById(id).innerHTML;
document.getElementById(id).attribute = new value;
var attributeValue = document.getElementById(id).attribute;
onclick - Fires on a mouse click on the element
onblur - Fires the moment that the element loses focus
onchange - Fires the moment when the value of the element is changed
onload - Fires after the page is finished loading
<script>
myFunction = function() {
alert("Button clicked");
}
</script>
<button style="width:100px;" onclick=myFunction()> Click me</button>