码迷,mamicode.com
首页 > Web开发 > 详细

JS - Introduction

时间:2016-07-04 22:14:09      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

What can we use JavaScript for?

  • make websites respond to user interaction
  • build apps and games
  • access information on the Internet
  • organize and present data

You can do pretty much anything with JavaScript. You‘ll start small with simple features such as carousels, image galleries, fluctuating layouts, and responses to button clicks. Eventually as you get more experienced with the language, you‘ll be able to create games, animated 2D and 3D graphics, full blown database-driven apps, and more!

 

Note: The reason we‘ve put the <script> element near the bottom of the HTML file is that HTML is loaded by the browser in the order it appears in the file. If the JavaScript is loaded first and it is supposed to affect the HTML below it, it might not work, as the JavaScript would be loaded before the HTML it is supposed to work on. Therefore, putting JavaScript near the bottom of the HTML page is often the best strategy.

 

confirm("I feel awesome!");
confirm("I am ready to go.");

 

prompt("What is your name?");
prompt("what is Ubuntu?");

 

 

Data Types:

Variable Explaation Example
String A string of text. To signify that the variable is a string, you should enclose it in quote marks. var myVariable = ‘Bob‘;
Number A number. Numbers don‘t have quotes around them. var myVariable = 10;
Boolean A True/False value. The words true and false are special keywords in JS, and don‘t need quotes. var myVariable = true;
Array A structure that allows you to store multiple values in one single reference. var myVariable = [1,‘Bob‘,‘Steve‘,10];
Refer to each member of the array like this:
myVariable[0]myVariable[1], etc.
Object Basically, anything. Everything in JavaScript is an object, and can be stored in a variable. Keep this in mind as you learn. var myVariable = document.querySelector(‘h1‘);
All of the above examples too.

 

"David".length

 

console.log("Hello Wold");

 

 

if( "Qi Wei".length >= 7 ) {
    console.log("You have a long name!");
}
else {
    console.log("You have a short name!");  
}

 

 

Substrings

"hello".substring(0,2);

 

 

Variable:

Variables are containers that you can store values in. You use var keyword to declar. followed by any name you want to call it.

var myName = "Zhang Qi Wei";
var myAge = 30;
var isOdd = true;

Note: All statements in JavaScript must end with a semi-colon, to indicate that this is where the statement ends. If you don‘t include these, you can get unexpected results.

 

Comments

You can put comments into JavaScript code, just like you can in CSS.

//  or  /**/

 

Operators

+ - * / 

assignment operator  = 

identity operator ===

Negation, not equal ! , !==

 

Conditionals

Conditionals are code structures that allow you to test whether an expression returns true or not, and then run different code depending on the result. 

var iceCream = ‘chocolate‘;
if (iceCream === ‘chocolate‘) {
  alert(‘Yay, I love chocolate ice cream!‘);    
} else {
  alert(‘Awwww, but chocolate is my favorite...‘);    
}

 

 

Functions

Functions are a way of packaging functionality that you want to reuse, so that whenever you want the functionality you can call the function with the function name rather than constantly rewriting the entire code. 

 

function multiply(num1,num2) {
  var result = num1 * num2;
  return result;
}

 

 

Events

To create real interactivity on a website, you need events. these are code structures that listen out for things happening to the browser, and then allow you to run code in response to those things. 

document.querySelector(‘html‘).onclick = function() {
    alert(‘Ouch! Stop poking me!‘);
}

 

 Resources to learn:

MDN: https://developer.mozilla.org/en-US/   

the odin project

 

JS - Introduction

标签:

原文地址:http://www.cnblogs.com/elewei/p/5641604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!