js运行是不需要搭建环境的,可以编写好js脚本后,采用下列步骤进行运行。
创建html文件
使用语法引入外部js文件
然后双击html
在浏览器中就可以看到运行js的效果了。
例如:
html文件内容如下:
<script type="text/javascript" src="test.js"></script>
<body>
</body>
js文件内容如下:
alert(1);
运行的结果就是会在浏览器中弹出提示框显示1
javascript 实现冒泡排序:
打开webstorm
创建js文件:
bubbleSort.js
function bubbleSort(arr) {
var i = arr.length, j;
var tempExchangVal;
while (i > 0) {
for (j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tempExchangVal = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tempExchangVal;
}
}
i--;
}
return arr;
}
var arr = [3, 2, 4, 9, 1, 5, 7, 6, 8];
var arrSorted = bubbleSort(arr);
console.log(arrSorted);
alert(arrSorted);
创建bubbleSort.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>冒泡排序--javascript</title>
</head>
<script type="text/javascript" src="bubbleSort.js"></script>
<body>
</body>
</html>
运行:
(9) [1, 2, 3, 4, 5, 6, 7, 8, 9]